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

From: Shawn Smith (shawn.p.smith at gmail.com)
Date: Mon May 14 00:01:29 EDT 2012

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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120513/4ef9db89/attachment-0001.html>

Posted on the users mailing list.