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

From: Shawn Smith (shawn.p.smith at gmail.com)
Date: Mon May 14 05:26:11 EDT 2012

Update: Found out I can do this:

https://github.com/plt/racket/wiki/Artifacts

(parameterize ([current-input-port (open-input-string "We are Spinal Tap!")])
  (with-output-to-string (λ _ (system* (build-path "c:"
"openssl-win32" "bin" "openssl.exe")
                                      "dgst"
                                      "-sha1"
                                      "-hex"
                                      "-hmac"
                                      #"mysecretkey"))))


On Mon, May 14, 2012 at 1:12 AM, Shawn Smith <shawn.p.smith at gmail.com>wrote:

> I guess I've tracked the problem down to the fact that the Racket hash is
> not a digest.
>
> I don't really know what that means.
>
> If I do:
>
> 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?
>
> On Sun, May 13, 2012 at 9:32 PM, Veer Singh <veer.chah at gmail.com> wrote:
>
>> 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
>> >
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120514/c1328542/attachment-0001.html>

Posted on the users mailing list.