xsc/pandect

get string from hmac

nha opened this issue · 2 comments

nha commented

Hello,

Sorry for posting here, I'm not sure where to ask that, and I've been trying to get something working for quite some time...

I have trouble getting the String from the sha256-hmac-bytes function.
Here is a small example :

  (let [signature (sha256-hmac-bytes "test" "my-secret")]
    (println signature)     ;; #bin Oz+h3YkA8vHXQAgBst2NxZBRpmczdV8AEoadoTluGA4=

    ;; I can see it above !!
    ;; How do I return get the string "Oz+h3YkA8vHXQAgBst2NxZBRpmczdV8AEoadoTluGA4" above ?
    ;; instead of "test.Oz+h3YkA8vHXQAgBst2NxZBRpmczdV8AEoadoTluGA4"  I get "test.[B@5db4cdc7"
    ;; Is it possible to do that ?
    (str "test." signature))

Aditionally, I tried things like :

    (println (String. signature))               ;?�݉�����ݍŐQ�g3u_����9n�
    (println (String. signature "UTF-8"))       ;?�݉�����ݍŐQ�g3u_����9n�
    (println (String. signature "ASCII"))       ;?������������Q�g3u_����9n�
    (println (String. signature "ISO8859_1"))   ;?¡Ý�òñ×�²Ý�Å�Q¦g3u_���¡9n�

Thank you for your help

xsc commented

The encoding you're looking for is Base64. You can achieve your desired result by adding e.g. the following dependency:

[org.clojure/data.codec "0.1.0"]

Then:

(require '[pandect.algo.sha256 :refer [sha256-hmac-bytes]]
         '[clojure.data.codec.base64 :as b64])

(-> (sha256-hmac-bytes "test" "my-secret")
    (b64/encode)
    (String. "UTF-8"))
;; => "Oz+h3YkA8vHXQAgBst2NxZBRpmczdV8AEoadoTluGA4="

I hope that helps. Btw, I can't seem to achieve the output #bin "..." - is that some plugin doing that?

nha commented

Thank you a ton !

I don't use any special plugin for the output though (although I have quite a few plugins in my lein profile (below)). This is what confused me I think.

; https://github.com/technomancy/leiningen/wiki/Plugins
{ :user { :plugins [[cider/cider-nrepl "0.8.2"]         ; cider-jack-in
                    [lein-cljsbuild "1.0.4"]            ;; 1.0.4 is a requirement
                    ; [lein-figwheel "0.2.5"]             ; browser reload
                    [lein-figwheel "0.2.5" :exclusions [org.clojure/core.cache]]

                    ;[lein-drip "0.1.1-SNAPSHOT"]        ; faster JVM loading
                    [lein-midje "3.1.3"]                ; tests
                    [lein-exec "0.3.4"]                 ; execute a single .clj file
                    [lein-ancient "0.6.5"]              ; detect outdated dependencies
                    [lein-typed "0.3.5"]                ; core.typed plugin
                    [lein-kibit "0.0.8"]               
                    [lein-vanity "0.2.0"]
                    [lein-hiera "0.9.0"]                ; image of deps
                    [lein-bikeshed "0.2.0"]
                    [venantius/ultra "0.1.9"]
                    [com.jakemccrary/lein-test-refresh "0.9.0"] ; lein test-refresh
                [lein-auto "0.1.2"]                         ; lein auto test
]
         :ultra {:color-scheme :solarized_dark}}}

Thank you again !