我想计算 4 位数字的 sha256 哈希函数

I want to calculate the sha256 hashed function for 4digits numbers

我想计算 4 位密码的散列格式并将其保存到字典中以将其与我拥有的密码文件相匹配。但有一个问题。你能帮我吗?

Hashed_Passwords=list()

for i in range(1000,1010):
    Hashed=sha256(i).hexdigest()
        Four_digits_hashed.append(Hashed)

print(Four_digits_hashed)

哈希算法适用于原始字节,而不是 ints。如果你只想散列 int 的字符串化形式,请执行:

Hashed=sha256(str(i).encode()).hexdigest()

int 字符串化,然后将其编码为 UTF-8 bytes(在这种情况下相当于 ASCII,因为字符串形式仅使用 ASCII 数字)以格式 sha256明白了。

您需要先将整数编码成字符串:

from hashlib import sha256
from pprint import PrettyPrinter

four_digits_hashed = {}
for i in range(1000, 1010):
    hashed = sha256(str(i).encode('ASCII')).hexdigest()
    four_digits_hashed[i] = hashed

PrettyPrinter().pprint(four_digits_hashed)

输出:

{1000: '40510175845988f13f6162ed8526f0b09f73384467fa855e1e79b44a56562a58',
 1001: 'fe675fe7aaee830b6fed09b64e034f84dcbdaeb429d9cccd4ebb90e15af8dd71',
 1002: 'b281bc2c616cb3c3a097215fdc9397ae87e6e06b156cc34e656be7a1a9ce8839',
 1003: '8c9a013ab70c0434313e3e881c310b9ff24aff1075255ceede3f2c239c231623',
 1004: '75992a5ac67ff644d3063976c2effd10bdd93fcc109798e3d5c1acf2e530d01a',
 1005: '7f861bcee185de001377d79e08af62e94b1e7718e2470e08520c917f8d953602',
 1006: '478c4ffb1cbcea37956a748e6c19d8eadd0a47e86f5e308d26cad39453b5d1ab',
 1007: '2c8b871e52d4e5f5db5ff84a82a45327e20df77edef961c4b6fa0e9c3d97ce5b',
 1008: '9aaf689fbcdfe9f64a071f9cbe28ae44193fa218e72af24456f44bed64583b4d',
 1009: '6ad4a6b1e5ea5569795e516d71909e0ce4809d9dc983d2c219144f684f816e12'}