On 6 October 2011 19:38, Ray Miller <r...@1729.org.uk> wrote:
>
> Incidentally, I used the Apache Commons Base64 encoder, as the one in
> contrib was producing different results from the Apache Commons and
> Perl implementations. Perhaps a bug?
>

Here's the problem I alluded to above. I'm trying to compute the
base64 MD5 digest of the string "foobar". With Perl, I'd do it like
this:

   perl -MDigest::MD5=md5_base64 -le 'print md5_base64("foobar")'

Which gives me the string: "OFj2IjCsPJFfMAxmQxLGPw"

Now in Clojure, I can use java.security.MessageDigest to give me the
MD5 digest as a byte array. When I transform this to base64 using the
Apache Commons library, I get the same result as above. But when I try
to do it with clojure.contrib.base64, I get a slightly different
string. It's quite possible I've misunderstood the
clojure.contrib.base64 API (in which case enlightenment is welcome!),
but I wondered if I'd tickled a bug in the contrib library?

Here's the code:

(use '[clojure.contrib.io :only (to-byte-array input-stream)])

(import '[java.security MessageDigest]
        '[java.io StringWriter]
        '[org.apache.commons.codec.binary Base64])

(require '[clojure.contrib.base64 :as base64])

(def md5sum (.. (java.security.MessageDigest/getInstance "MD5")
                (digest (to-byte-array "foobar"))))
;;=> #<byte[] [B@6fd33eef>

(org.apache.commons.codec.binary.Base64/encodeBase64String md5sum)
;;=> "OFj2IjCsPJFfMAxmQxLGPw=="

(let [output (StringWriter.)]
  (base64/encode (input-stream md5sum)
                 output
                 base64/*base64-alphabet*
                 nil)
  (.toString output))
;;=> "OF/2Ij+sP5FfMAxmQx/GPw=="

This string differs in several positions (first at character 3). Any
idea what's going wrong?

Ray.

-- 
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

Reply via email to