Blake2b hashing
I was using this library: https://github.com/k3d3/ed25519-java but its using BitIntegers and was very slow. I want to use a Blake2b hash with ED25519 to sign some keys, and I was wondering how I can do this? I'm thinking I make a Blake2b MessageDigest class and then define an EdDsaNamedCurve spec using this hash, correct? Sorry, I could not find any example code doing this.
Damn, well that was fun. I was able to get it to work using a variety of documentation ranging from:
- https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html
- https://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#Provider
- http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/security/MessageDigest.java
- https://docs.oracle.com/javase/7/docs/technotes/guides/security/crypto/HowToImplAProvider.html
- https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/CryptoSpec.html#Security
- https://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/HowToImplAProvider.html
- https://docstore.mik.ua/orelly/java-ent/security/ch09_03.htm
- https://docstore.mik.ua/orelly/java-ent/security/ch08_02.htm
Basically, this lead me into realizing what I needed to do in order to get an Ed25519 signing algorithm to play nicely with a Blake2b hashing algorithm. I used alphazero's Java port of Blake2b (here) in order to create a MessageDigest implementation and a Provider, I created three classes:
- Blake2bProvider, which was similar to EdDSASecurityProvider, except that it added this line to the map:
put("MessageDigest.Blake2b", "com.package.name.Blake2bMessageDigest") - Blake2bMessageDigest, which was basically a MessageDigest wrapper for Blake2b
- Ed25519Blake2bCurveSpec, which extended EdDSANamedCurveTable, and defined the hashing algorithm to be Blake2b where everything else was the same as a normal Ed25519CurveSpec
Once I created all of those, all I had to do was:
val provider = Blake2bProvider()
Security.addProvider(provider)
val blake2bMessageDigest = MessageDigest.getInstance("Blake2b")
val spec = Ed25519Blake2bCurveSpec().ed25519Blake2bCurveSpec
EdDSANamedCurveTable.defineCurve(spec)
val specAfterDefine = EdDSANamedCurveTable.getByName(spec.name)
For reference, I am working a nanocurrency mobile wallet written natively in Java/Kotlin. My work will soon be shared on my github account and I can share more progress / implementation details as I clean up my code. Thanks for the fast EdDSA library to work with!
Based in @schott12521 implementation I wrote this.
It would be really nice to have a easier way to achieve the same.