Search code examples
pythonsha256hashlib

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


I want to calculate the hashed format of the 4 digits password and save it into a dictionary to match it with the password files that I have. But there is a problem. can you help me with this?

Hashed_Passwords=list()

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

print(Four_digits_hashed)

Solution

  • You need to encode the integer into a string first:

    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)
    

    Output:

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