Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53357 > unrolled thread
| Started by | sergio7654@gmail.com |
|---|---|
| First post | 2013-08-31 07:24 -0700 |
| Last post | 2013-08-31 08:16 -0700 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
HMAC encription issue sergio7654@gmail.com - 2013-08-31 07:24 -0700
Re: HMAC encription issue Roy Smith <roy@panix.com> - 2013-08-31 10:39 -0400
Re: HMAC encription issue Sergio Sanchez <sergio7654@gmail.com> - 2013-08-31 07:48 -0700
Re: HMAC encription issue Roy Smith <roy@panix.com> - 2013-08-31 11:01 -0400
Re: HMAC encription issue Sergio Sanchez <sergio7654@gmail.com> - 2013-08-31 08:16 -0700
| From | sergio7654@gmail.com |
|---|---|
| Date | 2013-08-31 07:24 -0700 |
| Subject | HMAC encription issue |
| Message-ID | <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com> |
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!
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-08-31 10:39 -0400 |
| Message-ID | <roy-27E004.10390831082013@news.panix.com> |
| In reply to | #53357 |
In article <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com>, sergio7654@gmail.com wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Sergio Sanchez <sergio7654@gmail.com> |
|---|---|
| Date | 2013-08-31 07:48 -0700 |
| Message-ID | <bf1666dc-8add-4472-b3a4-6a7c4ec0d357@googlegroups.com> |
| In reply to | #53358 |
El sábado, 31 de agosto de 2013 16:39:08 UTC+2, Roy Smith escribió: > In article <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com>, > > sergio7654@gmail.com wrote: > > > > > 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. Sorry, that was a problem with the cut & paste. The two sentences key="4545453030303743303134462035343733373432363420323031332D30382D33312031343A33353A3035" are just the same in Python and Javascript.
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-08-31 11:01 -0400 |
| Message-ID | <roy-673BDB.11010131082013@news.panix.com> |
| In reply to | #53359 |
In article <bf1666dc-8add-4472-b3a4-6a7c4ec0d357@googlegroups.com>, Sergio Sanchez <sergio7654@gmail.com> wrote: > > To start with, your keys are not the same > > Sorry, that was a problem with the cut & paste. 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.
[toc] | [prev] | [next] | [standalone]
| From | Sergio Sanchez <sergio7654@gmail.com> |
|---|---|
| Date | 2013-08-31 08:16 -0700 |
| Message-ID | <a77c03ae-bb79-4e80-aa73-9231150a909e@googlegroups.com> |
| In reply to | #53359 |
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ó: > > > In article <3479e08e-d435-492b-b2a0-a1c18678f6ab@googlegroups.com>, > > > > > > sergio7654@gmail.com wrote: > > > > > > > > > > > > > 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. > > > > > > 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,
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web