views:

62

answers:

2

when i run this from the repl:

(def md (MessageDigest/getInstance "SHA-1"))
(. md update (into-array [(byte 1)  (byte 2)  (byte 3)]))

I get:

No matching method found: update for class java.security.MessageDigest$Delegate

the Java 6 docs for MessageDigest show:

update(byte[] input) 
      Updates the digest using the specified array of bytes.

and the class of (class (into-array [(byte 1) (byte 2) (byte 3)])) is [Ljava.lang.Byte;

Am I missing something in the definition of update?
Not creating the class I think I am?
Not passing it the type I think I am?

+3  A: 

Because you are calling update(Byte[]) which is not defined in MessageDigest. You need to convert it into primitive array.

You can do something like this,

 (defn updateBytes [#^MessageDigest md, #^bytes data] 
      (.update md data)) 
ZZ Coder
(. md update #^bytes (into-array [(byte 1) (byte 2) (byte 3)])) produces the same result. is "bytes" a name for the type-of-an-array-of-bytes?
Arthur Ulfeldt
#^bytes is a type hint, not an assertion or a cast or a declaration. Hints only affect whether reflection is avoided when an object that matches the type hint is passed as the argument -- it does not yield an error when an object of an unrelated type is passed.
Chas Emerick
+2  A: 

Try:

(. md update (into-array Byte/TYPE [(byte 1) (byte 2) (byte 3)]))
Greg Harman
Awsome! You got me unstuck! I do wonder why into-array didn't detect the type of the first entry?
Arthur Ulfeldt
Well I think @ZZCoder had that right below - it may have been detecting the Byte class rather than the primitive.
Greg Harman
yes that would explain it. That lead me to test and fix this when hashing int vs. Integer also.
Arthur Ulfeldt