[racket] string->bytes/locale on an HMAC-SHA1 hash returns error

From: Veer Singh (veer.chah at gmail.com)
Date: Mon May 14 00:32:34 EDT 2012

In racket you are applying function "base64-encode" and in python you are not.

Sorry if I didn't get your question.

Veer.

On Mon, May 14, 2012 at 9:38 AM, Shawn Smith <shawn.p.smith at gmail.com> wrote:
> Having issues with mailing list.  Trying again:
>
> I've gotten a bit of help in #racket on freenode so far, but I'm still
> facing a problem with this.
>
> In Racket, I expect that this will get me a SHA1 hash (as bytes) of a
> string, using a key:
>
> (require web-server/stuffers/hmac-sha1
>             net/base64)
>
> (define (str-to-hash private-key str)
>    (base64-encode (HMAC-SHA1 (string->bytes/utf-8 private-key)
>                                                 (string->bytes/utf-8 str))))
>
> And let's convert the bytes to a string of hex:
>
> (apply string-append (map (lambda (x) (number->string x 16)) (bytes->list
> (str-to-hash "foo" "bar"))))
>
> This gives me:
>
> "527254735747455846553273314a316d546c316a2f63694f2b31453dda"
>
> In Python, doing what I assume to be the same thing:
>
> import hmac
> from hashlib import sha1
>
> hmac.new('foo', 'bar', sha1).hexdigest()
>
> Returns something completely different:
>
> '46b4ec586117154dacd49d664e5d63fdc88efb51'
>
> Does anyone have any idea what's going on here?
>
> On Sun, May 13, 2012 at 9:01 PM, Shawn Smith <shawn.p.smith at gmail.com>
> wrote:
>>
>> I've gotten a bit of help in #racket on freenode so far, but I'm still
>> facing a problem with this.
>>
>> In Racket, I expect that this will get me a SHA1 hash (as bytes) of a
>> string, using a key:
>>
>> (require web-server/stuffers/hmac-sha1
>>             net/base64)
>>
>> (define (str-to-hash private-key str)
>>    (base64-encode (HMAC-SHA1 (string->bytes/utf-8 private-key)
>>                                                 (string->bytes/utf-8
>> str))))
>>
>> And let's convert the bytes to a string of hex:
>>
>> (apply string-append (map (lambda (x) (number->string x 16)) (bytes->list
>> (str-to-hash "foo" "bar"))))
>>
>> This gives me:
>>
>> "527254735747455846553273314a316d546c316a2f63694f2b31453dda"
>>
>> In Python, doing what I assume to be the same thing:
>>
>> import hmac
>> from hashlib import sha1
>>
>> hmac.new('foo', 'bar', sha1).hexdigest()
>>
>> Returns something completely different:
>>
>> '46b4ec586117154dacd49d664e5d63fdc88efb51'
>>
>> Does anyone have any idea what's going on here?
>>
>> Shawn
>>
>> On Sun, May 13, 2012 at 7:18 PM, Shawn Smith <shawn.p.smith at gmail.com>
>> wrote:
>>>
>>> Hi,
>>>
>>> I'm trying to generate a SHA1 hash using a private key and a given
>>> string, like so:
>>>
>>> (define a-hash (HMAC-SHA1 (string->bytes/locale "foo")
>>> (string->bytes/locale "bar")))
>>>
>>> This returns bytes, but when I try to turn those bytes into a string
>>> with:
>>>
>>> (bytes->string/locale a-hash)
>>>
>>> I get:
>>>
>>> bytes->string/locale: string is not a well-formed UTF-8 encoding:
>>> #"F\264\354Xa\27\25M\254\324\235fN]c\375\310\216\373Q"
>>>
>>> As I have no clue how to diagnose this, I'd appreciate any suggestions or
>>> help.
>>>
>>> Thanks,
>>> Shawn
>>
>>
>
>
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users
>


Posted on the users mailing list.