tags:

views:

33

answers:

1

hi all

i have a javascript password coder

md5 = hex_hmac_md5(secret, password)

How can i emulate this in python - ive tried md5 but that is not the same value

i got my md5 javascript code from this website:

Pajs Home

(md5.js)

He states the use is as follows:

In many uses of hashes you end up wanting to combine a key with some data. It isn't so bad to do this by simple concatenation, but HMAC is specifically designed for this use. The usage is:

hash = hex_hmac_md5("key", "data");

The HMAC result is also available base-64 encoded or as a binary string, using b64_hmac_* or str_hmac_*.

Some other hash libraries have the arguments the other way round. If the JavaScript HMAC doesn't match the value your server library generates, try swapping the order.

I have tried some python like this:

> def md5_test(secret, password):
>     
>     return md5(secret+password).hexdigest()

Can anyone tell me what the code should be in python to get the same value?

Thanks

+1  A: 

That's what Python's hmac module is for, don't use the MD5 function directly.

# Right
import hmac
# Note that hmac.new defaults to using MD5
hmac.new("password", "message").hexdigest() # 'f37438341e3d22aa11b4b2e838120dcf'

# Wrong
from hashlib import md5
md5("message"+"password").hexdigest() # 'd0647ee3be62a57c9475541c378b1fac'
md5("password"+"message").hexdigest() # 'c494404d2dd827b05e27bd1f30a763d2'

Also take a look at how HMAC is implemented (e.g. on Wikipedia).

AndiDog
so to get the same value as the javascript code i wud use: mport hmachmac.new("secret","password").hexdigest(). Thanks.
spidee
It still does not return the same value as the javascript method ??
spidee
Sorry i had the secret and password around the wrong way in my calling method - it now works. Thank you very much, saved me hours!
spidee