title: Packing a 40 byte SHA in 20 bytes in Clojure
body: <p>To pack a 40 byte SHA in 20 bytes, we are doing this:</p>
  <pre><code>(defn pack-sha-1 [sha-1] 
    (-&gt;&gt; sha-1
         (partition 2)
         (map (partial apply str)) ;; To convert back to list of strings
         (map (fn [hex] (-&gt; hex
                           (Integer/parseInt 16)
                           char)))
         (apply str)))             ;; To convert back to string
  </code></pre>
  <p>First we partition by 2 and then convert that into a single character.</p>
  <p>This is essentially the packing step where 2 hex characters are converted into a
  single character.</p>
  <p>When we try to convert the packed string back to its 40 bytes SHA (using
  <a href="https://www.rapidtables.com/convert/number/ascii-to-hex.html" rel="nofollow noreferrer">https://www.rapidtables.com/convert/number/ascii-to-hex.html</a>), it does not give
  the same SHA back in all the cases.</p>
  <p>It works for: &quot;5e4fb7a0afe4f2ec9768a9ddd2c476dab7fd449b&quot;</p>
  <p>But it does not work for: &quot;00c35422185bf1dca594f699084525e8d0b8569f&quot;</p>
  <p>Whenever there is a pair of hex in the range (&quot;08&quot; - &quot;0d&quot;), it does not work.</p>
  <p>What is going wrong here?</p>
  <p>This is done as part of implementing James Coglan's book &quot;Building Git&quot; in Clojure.</p>
  <p>Thanks for your help!</p>