HMAC encription issue

S

sergio7654

Hi, I need to create a HMAC in Python just the way the following Javascript code does:

var credentials = "admin:amin";
key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035"
var shaObj = new jsSHA(credentials, "ASCII");
var hash = shaObj.getHMAC(key, "HEX", "HEX");

----

in this case the hash is: 8347ce7126c1068aa183fbfdafe2c17a9cc86734

This javascript library can be tested here: http://caligatio.github.io/jsSHA/


So I'm trying to do the same in Python by:

key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$

credentials = "admin:admin"

hash = hmac.new(key, credentials, hashlib.sha1).hexdigest()

-----

which gives me hash= 2c7e849a0eaa8cd0cc1120f6f156913755b674b6

Something's is wrong obviously. Maybe it has probably something to do with the encoding of the key or credentials but I'm lost on this.

I tried to convert key to hex using the following function

def toHex(s):
lst = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0'+hv
lst.append(hv)

return reduce(lambda x,y:x+y, lst)

But this didn't work either.


Any help would be appreciated!
 
R

Roy Smith

key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035"
key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A303$

To start with, your keys are not the same (the JS one ends in "35", the Python one ends in "3$",
and I don't know what to make of the fact that your Python key isn't terminated with a quote.
 
S

Sergio Sanchez

El sábado, 31 de agosto de 2013 16:39:08 UTC+2, Roy Smith escribió:
(e-mail address removed) wrote:







To start with, your keys are not the same (the JS one ends in "35", the Python one ends in "3$",

and I don't know what to make of the fact that your Python key isn't terminated with a quote.


Sorry, that was a problem with the cut & paste. The two sentences key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" are just the same in Python and Javascript.
 
R

Roy Smith

To start with, your keys are not the same

Sorry, that was a problem with the cut & paste.[/QUOTE]

And your credentials are different too:

var credentials = "admin:amin";
credentials = "admin:admin"

Is this also a cut & paste error?

Please, post EXACTlY the code you are actually running. Otherwise, it
really is impossible for anybody to figure out what you're doing wrong.
This is double ultra especially true with crypto, since small
differences in the inputs result (by design) in huge differences in the
output.
 
S

Sergio Sanchez

El sábado, 31 de agosto de 2013 16:48:53 UTC+2, Sergio Sanchez escribió:
El sábado, 31 de agosto de 2013 16:39:08 UTC+2, Roy Smith escribió:
Sorry, that was a problem with the cut & paste. The two sentences key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" are just the same in Python and Javascript.



Solved:

key must be converted to binary with the function unhexlify(hexstr)

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top