【问题标题】:Generate upper and lower password case password from hash从哈希生成大小写密码
【发布时间】:2017-10-15 19:49:47
【问题描述】:

我正在实现一个密码生成器,它根据服务名称和主密码创建密码。为此,主密码和服务被散列。现在我只有小写字母和数字。我怎么还能包含大写字母和特殊符号?

我找到了以下代码,似乎可以解决问题。我应该使用它吗?它到底有什么作用?

raw_hexdigest = make_password(plaintext, service)

# Convert the hexdigest into decimal
num = int(raw_hexdigest, 16)

# What base will we convert `num` into?
num_chars = len(alphabet)

# Build up the new password one "digit" at a time,
# up to a certain length
chars = []
while len(chars) < length:
    num, idx = divmod(num, num_chars)
    chars.append(alphabet[idx])

return ''.join(chars)

【问题讨论】:

    标签: python security hash passwords


    【解决方案1】:

    首先,请注意散列密码作为整数键返回。您返回的密码只是以某个基数表示的整数,带有给定的alphabet。关键是你提供的alphabet

    以 10 为底,字母为“01234356789”;对于十六进制,它是“01234356789ABCDEF”。看来您一直在使用 base 36:10 位数字和 26 个小写字母。

    要扩展它,只需将 alphabet 指定为您想要的密码字符。

    更详细地说,底部的循环是逐位基础转换。 base 是字母表的长度。在每次迭代中,循环使用 divmod 将密钥整数拆分为该基数 (idx) 中的“单位”值和商,即整数的其余部分。然后它从字母表中选择相应的字符并将其附加到一个字符串中,最终成为密码。

    我建议你插入一些战略性的print 语句并观察它是如何运行的……也许以 10 为底,然后以 16 为底。不要调用make_password,只需将密钥设置为你熟悉的东西,例如

    raw_hexdigest = 1024 + 256 + 4 + 1
    

    从那里,观察这个数字是如何在你给例程提供的各种字母中处理的。

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2021-04-05
      • 2016-02-06
      • 2014-02-24
      • 1970-01-01
      • 2015-05-12
      • 1970-01-01
      • 2018-08-08
      相关资源
      最近更新 更多