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

From: Norman Gray (norman at astro.gla.ac.uk)
Date: Mon May 14 05:23:54 EDT 2012

Shawn, hello.

On 2012 May 14, at 09:12, Shawn Smith wrote:

> I guess I've tracked the problem down to the fact that the Racket hash is
> not a digest.

I think it is, but the problem is that you're not displaying the resulting byte string correctly.

> echo -n "foo" | openssl dgst -sha1 -hmac "privatekey"
> I get what I'm looking for, a 40 character hex hash:
> 6d1198d3c8770f2409c6b73ed11af9d2076e4588
> I'm not sure how to get Racket to do this as well.  Does anyone know?

(require web-server/stuffers/hmac-sha1
         file/sha1) ; or openssl/sha1

(bytes->hex-string (HMAC-SHA1 #"privatekey" #"foo"))

=> "6d1198d3c8770f2409c6b73ed11af9d2076e4588"

HMAC-SHA1 produces the result as a byte string (which is different from a string).  Base-64 encoding writes out that string of bytes in a form which can be safely mailed -- that's not what you want.

Instead, bytes->hex-string formats each byte in the string as a two-digit hex number, resulting in a string.

All the best,


Norman Gray  :  http://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK

Posted on the users mailing list.