Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing with ironclad and flexi-streams

I'm trying to hash some strings in a Common Lisp app I'm working on. The sd-sha1 package seems to be unsupported, and has been for some time judging by the CLiki page, which suggests using Ironclad instead. Fair enough,

=> (require 'ironclad)
NIL

Ironclad doesn't do string digests though; this is stated on its project page as an intentional design choice, what I'm supposed to do is convert my string to a byte-string and hash that. In other words

=> (ironclad:digest-sequence
      :sha1 (flexi-streams:string-to-octets "Hello there"))
#(114 108 118 85 62 26 63 222 162 145 52 243 110 106 242 234 5 236 92 206)

Ok, now the thing is that the point of this entire excercise is to get out a sha1-hashed string of the original string input, which means that I really want to convert the above back into string format. But,

=> (flexi-streams:octets-to-string 
     (ironclad:digest-sequence
        :sha1 (flexi-streams:string-to-octets "Hello there"))
     :external-format :utf-8)

This sequence can't be decoded using UTF-8 as it is too short.  
1 octet missing at then end.
     [Condition of type FLEXI-STREAMS:EXTERNAL-FORMAT-ENCODING-ERROR]

Restarts:
     0: [ABORT] Exit debugger, returning to top level.

The other option is to let flexi-streams infer the correct encoding.

=> (flexi-streams:octets-to-string
      (ironclad:digest-sequence
        :sha1 (flexi-streams:string-to-octets "Hello there")))
"rlvU>?Þ¢4ónjòêì\\Î"

Which sort of works, but I get the feeling that the result isn't supposed to contain control characters. According to flexi-streams, the default encoding is :latin, so I'm really not sure what to do at this stage.

What am I doing wrong? How do I get a string-representation of a SHA1-digested string in Common Lisp?

In case it matters, I'm running SBCL (the version out of apt-get, which is 1.0.29 I believe) through Emacs+SLIME.

like image 971
Inaimathi Avatar asked Oct 10 '10 14:10

Inaimathi


1 Answers

The octets you get from ironclad:digest-sequence are the SHA1 digest. You want a string representing a hexadecimal encoding of those bytes. Ironclad has a function to do that built-in: ironclad:byte-array-to-hex-string.

like image 199
Xach Avatar answered Sep 19 '22 09:09

Xach