19mike95
19mike95

Reputation: 506

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?

Upvotes: 2

Views: 14872

Answers (1)

Adam
Adam

Reputation: 719

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/

Upvotes: 5

Related Questions