title: Hashing with ironclad and flexi-streams
body: <p>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, </p>
  
  <pre><code>=&gt; (require 'ironclad)
  NIL
  </code></pre>
  
  <p>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</p>
  
  <pre><code>=&gt; (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)
  </code></pre>
  
  <p>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,</p>
  
  <pre><code>=&gt; (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.
  </code></pre>
  
  <p>The other option is to let <code>flexi-streams</code> infer the correct encoding.</p>
  
  <pre><code>=&gt; (flexi-streams:octets-to-string
        (ironclad:digest-sequence
          :sha1 (flexi-streams:string-to-octets "Hello there")))
  "rlvU&gt;?Þ¢4ónjòêì\\Î"
  </code></pre>
  
  <p>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 <code>:latin</code>, so I'm really not sure what to do at this stage.</p>
  
  <p>What am I doing wrong? How do I get a string-representation of a SHA1-digested string in Common Lisp?</p>
  
  <p>In case it matters, I'm running SBCL (the version out of apt-get, which is 1.0.29 I believe) through Emacs+SLIME.</p>