Having issues with mailing list. Trying again:<div><br></div><div><span style>I've gotten a bit of help in #racket on freenode so far, but I'm still facing a problem with this.</span><div style><br></div><div style>
In Racket, I expect that this will get me a SHA1 hash (as bytes) of a string, using a key:</div><div style><br></div><div style><div>(require web-server/stuffers/hmac-sha1</div><div> net/base64)</div></div><div style>
<br></div><div style><div>(define (str-to-hash private-key str)</div><div> (base64-encode (HMAC-SHA1 (string->bytes/utf-8 private-key)</div><div> (string->bytes/utf-8 str))))</div>
</div><div style><br></div><div style>And let's convert the bytes to a string of hex:</div><div style><br></div><div style>(apply string-append (map (lambda (x) (number->string x 16)) (bytes->list (str-to-hash "foo" "bar"))))</div>
<div style><br></div><div style>This gives me:</div><div style><br></div><div style>"527254735747455846553273314a316d546c316a2f63694f2b31453dda"</div><div style><br></div><div style>In Python, doing what I assume to be the same thing:</div>
<div style><br></div><div style>import hmac</div><div style>from hashlib import sha1</div><div style><br></div><div style><div>hmac.new('foo', 'bar', sha1).hexdigest()</div><div><br></div><div>Returns something completely different:</div>
<div><br></div><div>'46b4ec586117154dacd49d664e5d63fdc88efb51'</div><div><br></div><div>Does anyone have any idea what's going on here?</div></div><br><div class="gmail_quote">On Sun, May 13, 2012 at 9:01 PM, Shawn Smith <span dir="ltr"><<a href="mailto:shawn.p.smith@gmail.com" target="_blank">shawn.p.smith@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I've gotten a bit of help in #racket on freenode so far, but I'm still facing a problem with this.<div><br></div>
<div>In Racket, I expect that this will get me a SHA1 hash (as bytes) of a string, using a key:</div>
<div><br></div><div><div>(require web-server/stuffers/hmac-sha1</div><div> net/base64)</div></div><div><br></div><div><div>(define (str-to-hash private-key str)</div><div> (base64-encode (HMAC-SHA1 (string->bytes/utf-8 private-key)</div>
<div> (string->bytes/utf-8 str))))</div></div><div><br></div><div>And let's convert the bytes to a string of hex:</div><div><br></div><div>(apply string-append (map (lambda (x) (number->string x 16)) (bytes->list (str-to-hash "foo" "bar"))))</div>
<div><br></div><div>This gives me:</div><div><br></div><div><div>"527254735747455846553273314a316d546c316a2f63694f2b31453dda"</div></div><div><br></div><div>In Python, doing what I assume to be the same thing:</div>
<div><br></div><div>import hmac</div><div>from hashlib import sha1</div><div><br></div><div><div>hmac.new('foo', 'bar', sha1).hexdigest()</div><div><br></div><div>Returns something completely different:</div>
<div><br></div><div>'46b4ec586117154dacd49d664e5d63fdc88efb51'</div><div><br></div><div>Does anyone have any idea what's going on here?</div><span class="HOEnZb"><font color="#888888"><div><br>Shawn</div></font></span><div>
<div class="h5"><br><div class="gmail_quote">On Sun, May 13, 2012 at 7:18 PM, Shawn Smith <span dir="ltr"><<a href="mailto:shawn.p.smith@gmail.com" target="_blank">shawn.p.smith@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<div><br></div><div>I'm trying to generate a SHA1 hash using a private key and a given string, like so:</div><div>
<br></div><div>(define a-hash (HMAC-SHA1 (string->bytes/locale "foo") (string->bytes/locale "bar")))</div>
<div><br></div><div>This returns bytes, but when I try to turn those bytes into a string with:</div><div><br></div><div>(bytes->string/locale a-hash)</div><div><br></div><div>I get:</div><div><br></div><div>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"</div>
<div><br></div><div>As I have no clue how to diagnose this, I'd appreciate any suggestions or help.</div><div><br></div><div>Thanks,<br>Shawn</div>
</blockquote></div><br></div></div></div>
</blockquote></div><br></div>