<span style>I guess I've tracked the problem down to the fact that the Racket hash is not a digest.</span><div style><br></div><div style>I don't really know what that means.</div><div style><br></div><div style>If I do:</div>
<div style><br></div><div style>echo -n "foo" | openssl dgst -sha1 -hmac "privatekey"</div><div style><br></div><div style>I get what I'm looking for, a 40 character hex hash: </div><div style><br>
</div><div style>6d1198d3c8770f2409c6b73ed11af9d2076e4588</div><div style><br></div><div style>I'm not sure how to get Racket to do this as well. Does anyone know?</div><br><div class="gmail_quote">On Sun, May 13, 2012 at 9:32 PM, Veer Singh <span dir="ltr"><<a href="mailto:veer.chah@gmail.com" target="_blank">veer.chah@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">In racket you are applying function "base64-encode" and in python you are not.<br>
<br>
Sorry if I didn't get your question.<br>
<br>
Veer.<br>
<div><div class="h5"><br>
On Mon, May 14, 2012 at 9:38 AM, Shawn Smith <<a href="mailto:shawn.p.smith@gmail.com">shawn.p.smith@gmail.com</a>> wrote:<br>
> Having issues with mailing list. Trying again:<br>
><br>
> I've gotten a bit of help in #racket on freenode so far, but I'm still<br>
> facing a problem with this.<br>
><br>
> In Racket, I expect that this will get me a SHA1 hash (as bytes) of a<br>
> string, using a key:<br>
><br>
> (require web-server/stuffers/hmac-sha1<br>
> net/base64)<br>
><br>
> (define (str-to-hash private-key str)<br>
> (base64-encode (HMAC-SHA1 (string->bytes/utf-8 private-key)<br>
> (string->bytes/utf-8 str))))<br>
><br>
> And let's convert the bytes to a string of hex:<br>
><br>
> (apply string-append (map (lambda (x) (number->string x 16)) (bytes->list<br>
> (str-to-hash "foo" "bar"))))<br>
><br>
> This gives me:<br>
><br>
> "527254735747455846553273314a316d546c316a2f63694f2b31453dda"<br>
><br>
> In Python, doing what I assume to be the same thing:<br>
><br>
> import hmac<br>
> from hashlib import sha1<br>
><br>
> hmac.new('foo', 'bar', sha1).hexdigest()<br>
><br>
> Returns something completely different:<br>
><br>
> '46b4ec586117154dacd49d664e5d63fdc88efb51'<br>
><br>
> Does anyone have any idea what's going on here?<br>
><br>
> On Sun, May 13, 2012 at 9:01 PM, Shawn Smith <<a href="mailto:shawn.p.smith@gmail.com">shawn.p.smith@gmail.com</a>><br>
> wrote:<br>
>><br>
>> I've gotten a bit of help in #racket on freenode so far, but I'm still<br>
>> facing a problem with this.<br>
>><br>
>> In Racket, I expect that this will get me a SHA1 hash (as bytes) of a<br>
>> string, using a key:<br>
>><br>
>> (require web-server/stuffers/hmac-sha1<br>
>> net/base64)<br>
>><br>
>> (define (str-to-hash private-key str)<br>
>> (base64-encode (HMAC-SHA1 (string->bytes/utf-8 private-key)<br>
>> (string->bytes/utf-8<br>
>> str))))<br>
>><br>
>> And let's convert the bytes to a string of hex:<br>
>><br>
>> (apply string-append (map (lambda (x) (number->string x 16)) (bytes->list<br>
>> (str-to-hash "foo" "bar"))))<br>
>><br>
>> This gives me:<br>
>><br>
>> "527254735747455846553273314a316d546c316a2f63694f2b31453dda"<br>
>><br>
>> In Python, doing what I assume to be the same thing:<br>
>><br>
>> import hmac<br>
>> from hashlib import sha1<br>
>><br>
>> hmac.new('foo', 'bar', sha1).hexdigest()<br>
>><br>
>> Returns something completely different:<br>
>><br>
>> '46b4ec586117154dacd49d664e5d63fdc88efb51'<br>
>><br>
>> Does anyone have any idea what's going on here?<br>
>><br>
>> Shawn<br>
>><br>
>> On Sun, May 13, 2012 at 7:18 PM, Shawn Smith <<a href="mailto:shawn.p.smith@gmail.com">shawn.p.smith@gmail.com</a>><br>
>> wrote:<br>
>>><br>
>>> Hi,<br>
>>><br>
>>> I'm trying to generate a SHA1 hash using a private key and a given<br>
>>> string, like so:<br>
>>><br>
>>> (define a-hash (HMAC-SHA1 (string->bytes/locale "foo")<br>
>>> (string->bytes/locale "bar")))<br>
>>><br>
>>> This returns bytes, but when I try to turn those bytes into a string<br>
>>> with:<br>
>>><br>
>>> (bytes->string/locale a-hash)<br>
>>><br>
>>> I get:<br>
>>><br>
>>> bytes->string/locale: string is not a well-formed UTF-8 encoding:<br>
>>> #"F\264\354Xa\27\25M\254\324\235fN]c\375\310\216\373Q"<br>
>>><br>
>>> As I have no clue how to diagnose this, I'd appreciate any suggestions or<br>
>>> help.<br>
>>><br>
>>> Thanks,<br>
>>> Shawn<br>
>><br>
>><br>
><br>
><br>
</div></div>> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
</blockquote></div><br>