Unexpected result of decoding version 64 in Clojure

I demanded:

[clojure.data.codec.base64 :as b64]

I defined a function:

(defn toHexString [bytes]
  "Convert bytes to a String"
  (apply str (map #(format "%x" %) bytes)))

I run this code to get the result in Clojure:

(toHexString (b64/decode (.getBytes "WR0frsVTzVg8QdA9l45/MuYZ3GUKGynDF7WaEYcjudI")))

I also run this code in PHP for comparison (I'm sure the PHP result is correct):

echo bin2hex(base64_decode("WR0frsVTzVg8QdA9l45/MuYZ3GUKGynDF7WaEYcjudI"));

And I get these results for Clojure and PHP respectively:

591d1faec553cd583c41d03d978e7f32e619dc65a1b29c317b59a118723
591d1faec553cd583c41d03d978e7f32e619dc650a1b29c317b59a118723b9d2

Note that the result is almost exactly the same. The only difference is the absence of a 0 in the middle and four missing characters at the end of the Clojure result.

I do not know what I am doing wrong here. Is base64 decoding function in Clojure broken? Or what am I doing wrong? Thanks in advance!

+4
source share
1 answer

toHexString 0-pad , :

 (defn to-hex-string [bytes] (apply str (map #(format "%02x" %) bytes)))

base64 43, 4, , . , PHP Clojure (). Pad "=".

(to-hex-string (b64/decode (.getBytes "WR0frsVTzVg8QdA9l45/MuYZ3GUKGynDF7WaEYcjudI=")))

.

+4

Source: https://habr.com/ru/post/1536587/


All Articles