Just a quick guess but it seems like the ruby version base64 encodes the 
BINARY version of the digest, while the clojure version encodes the HEX 
version of the digest.

The sign function also converts to hex, so if you put that into base64 
encode you get the same (wrong) result, use the returned bytes of .doFinal 
and use that to generate the base64 and you should be fine.

HTH,
/thomas

On Friday, February 7, 2014 10:08:27 AM UTC+1, babysnakes wrote:
>
> Hi
>
> I'm trying to interface with a web service that expects a base64 code 
> produced as followed (I included both the direct output of digest and the 
> base64 encoding):
>
> require 'openssl'
> digest = OpenSSL::Digest::Digest.new('sha256')
> OpenSSL::HMAC.digest(digest, "key", "string")
> => 
> "\x97\xD1[\xEA\xBA\x06\r\a8\xECu\x9E\xA3\x18e\x17\x8A\xB8\xBBx\e-!\adK\xA8\x81\xF3\x99\xD8\xD6"
> Base64.encode64(OpenSSL::HMAC.digest(digest, "key", "string"))
> => "l9Fb6roGDQc47HWeoxhlF4q4u3gbLSEHZEuogfOZ2NY=\n"
>
>
> I need to send the base64 encoded string to the web service. I tried with 
> pandec and base64-clj:
>
> ✓ ~ ➤ lein try [pandect] [base64-clj]
> ...
> user=> (require '[base64-clj.core :as base64])
> nil
> user=> (require '[pandect.core :as p])
> nil
> user=> (p/sha256-hmac "string" "key")
> "97d15beaba060d0738ec759ea31865178ab8bb781b2d2107644ba881f399d8d6"
> user=> (base64/encode (p/sha256-hmac "string" "key"))
>
> "OTdkMTViZWFiYTA2MGQwNzM4ZWM3NTllYTMxODY1MTc4YWI4YmI3ODFiMmQyMTA3NjQ0YmE4ODFmMzk5ZDhkNg=="
>
> As you can see the generated sha-256 is similar to the ruby digest, but 
> it's not the same and the base64 is completely different. I've also tried 
> using javax.crypto.Mac directly but it's the same:
>
> (defn- secret-key-inst [key mac]
>   (SecretKeySpec. (.getBytes key) (.getAlgorithm mac)))
>
> (defn- sign* [key string]
>   "Returns the signature of a string with a given
>   key, using a SHA-256 HMAC."
>   (let [mac (Mac/getInstance "HMACSHA256")
>         secretKey (secret-key-inst key mac)]
>     (-> (doto mac
>           (.init secretKey)
>           (.update (.getBytes string)))
>         .doFinal)))
>
> (defn sign [key string]
>   (let [bytes (sign* key string)]
>     (apply str (map #(format "2%x" %) bytes))))
>
> Does anybody has an idea of how can I produce the same result as ruby? I 
> really don't want to resort to calling jruby snippet from clojure :(
>
> Thanks
> -- 
> Haim
>  

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to