funcool / buddy-sign

High level message signing library.
https://funcool.github.io/buddy-sign/latest/
Apache License 2.0
121 stars 45 forks source link

I got error when I try a minimal example of signing key #17

Closed ghost closed 8 years ago

ghost commented 8 years ago

I got this error while trying to sign resource with dsa and rsa keys:

backend.utils.jwt=> CompilerException java.lang.IllegalArgumentException: No implementation of method: :->byte-array of protocol: #'buddy.core.codecs/ByteArray found for class: org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey, compiling:(form-init4451766785895400468.clj:1:12)
niwinz commented 8 years ago

Can you provide a little bit more context, a little sample code that fails? I telling that because it there are unit test for rsa/dsa https://github.com/funcool/buddy-sign/blob/master/test/buddy/sign/jws_tests.clj#L132 that passes properly.

ghost commented 8 years ago

Here is my minimal example:

(ns backend.utils.jwt
  (:require
   [clojure.java.io :as io]
   [clj-time.core :as t]
   [buddy.sign.jws :as jws]
   [buddy.core.keys :as keys]))

(defn- private-key []
  (keys/private-key
   (io/resource "keys/ecprivkey.pem")))

(defn- public-key []
  (keys/public-key
   (io/resource "keys/ecpubkey.pem")))

(defn new-token
  [res]
  (let [exp (t/plus (t/now) (t/days 1))]
    (jws/sign res (private-key) {:exp exp})))

(defn from-token
  [token]
  (try
    (jws/unsign token (public-key))
    (catch Exception e
      (do (prn e)
          nil
          ))))

(comment
  (def token (new-token {:foo "bar"}))
  (jws/unsign token (pubkey))
  )

I hope there is no problem in conflicted deps, I have :pedantic? :abort on in my project.clj.

If I add custom alg when signing ( {:alg :es512} , I got his error when I unsign :

IllegalArgumentException No matching method found: initVerify for class java.security.Signature$Delegate  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80)
niwinz commented 8 years ago

I have missing specify the algoriththm:

(jws/sign res (private-key) {:alg :es256 :exp exp})))
niwinz commented 8 years ago

Using just your code and replacing the paths of the key pair and adding the :es256 or :es512 algorithm it just works.

ghost commented 8 years ago

yeah, I think it should work too. I doubt there are deps conflictions in my code base. hmm

niwinz commented 8 years ago

You have been able to solve the problem?

ghost commented 8 years ago

@niwinz I haven't retried it yet, work on another task. I will retry soon and report the results.

ghost commented 8 years ago

I think there is something wrong with my project, I'm not sure, I don't have time to debug the problem yet. But copying the example from you, everything works fine now. Thanks for helping :)

niwinz commented 8 years ago

Nice to know! Thanks!