简体   繁体   中英

MD5 hash in Python

I have been given a file with user and passwords in the format: $id$salt$hashed.

Where ID stands for the type of encryption and id=1 stands for FreeBSD-style MD5.

There is an example in which I know the password= "alice"

jsmith: $1$kDHTx$WKRXXT1P7UtjvU7CQ9eWs/:10063:0:99999:7:::

So I have done this in Python to check

import hashlib

passw='alice'
salt='kDHTx'

hashed= hashlib.md5(salt+passw).hexdigest()

print('What i get is: '+hashed)
print('What i should: '+'WKRXXT1P7UtjvU7CQ9eWs')

But I dont even get the format correctly:

What i get is: ba359e6dd36371c4dc5c187aac11e0d8
What i should: WKRXXT1P7UtjvU7CQ9eWs

What am I doing wrong? Or even understanding wrong from the begining?

You need to use the crypt library instead of hashlib .

>>> import crypt
>>> crypt.crypt('alice', crypt.METHOD_MD5)
$1$tlyP8ine$I9F3AiUCIgOjREqbx6WUg0

The salt is generated by the function when you pass in crypt.METHOD_MD5 .

To re-create an existing hash, you can pass it in as the second argument:

>>> crypt.crypt('alice', '$1$kDHTx$WKRXXT1P7UtjvU7CQ9eWs/')
$1$kDHTx$WKRXXT1P7UtjvU7CQ9eWs/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM