Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing a test hash with python pbkdf2_hmac-sha256 + salt not the same vaules

Tags:

python

sha256

For a encryption class i'm in we were tasked to make a program to check a password if the computer knows the following defined information. The teacher gave us a working set of fixed information.

I am trying to write a python code to verfiery a given set of user data based upon the known and fixed...

digest method: HMAC-SHA256

rounds: 100000

salt:"e65814e4382759f85550029e723dc7e7"

password:"governor washout beak"

to output the derived: "5f37a3bd08ac1c7d163294a3cb192ed1407b62bbc6a6259fee55f6e53f754273"

This is my code to generate the derived to check the against the above derived...

key = hashlib.pbkdf2_hmac('sha256', b'governor washout beak', b'e65814e4382759f85550029e723dc7e7', 100000, dklen=32)
print(binascii.hexlify(key))

however I get "0ce7d2e654c0ba80e67348c9610ca1851312458166ee8c9e6d46666832a21b11" instead. I don't understand what is missing.

like image 749
M Maslakowski Avatar asked Aug 31 '25 21:08

M Maslakowski


2 Answers

the error is the code takes the salt from ascii >> binary not hex >> binary so You want to use the binascii module:

import binascii

hexstr = 'e65814e4382759f85550029e723dc7e7'

binascii.unhexlify(hexstr)

b"\xe6X\x14\xe48'Y\xf8UP\x02\x9er=\xc7\xe7"
like image 79
M Maslakowski Avatar answered Sep 03 '25 10:09

M Maslakowski


In python 3, this can be written as follows:

hexstr = 'e65814e4382759f85550029e723dc7e7'
pbkdf2_hmac_key = hashlib.pbkdf2_hmac('sha256', b'governor washout beak', bytes.fromhex(hexstr), 100000, dklen=32)
print(pbkdf2_hmac_key.hex())
# 5f37a3bd08ac1c7d163294a3cb192ed1407b62bbc6a6259fee55f6e53f754273
like image 41
MSS Avatar answered Sep 03 '25 11:09

MSS