Both ed25519-java and js-nacl have an implementation of ed25519 crypto-signature methods. However, I obtained a public key and a signed message (signed using the public key's corresponding private key) from ed25519-java and tried verifying the signed message using the public key in js-nacl. This gave a null
value i.e. the signed message could not be opened with the public key.
My question is, shouldn't it be possible to sign in Java and verify the signature in Javascript? If so, or if not, why?
The Java code:
public static void main(String[] args) {
byte[] privateKey = new byte[32];
Arrays.fill(privateKey, (byte) 0);
byte[] publicKey = ed25519.publickey(privateKey);
byte[] signature = ed25519.signature("www.example.com".getBytes(), privateKey, publicKey);
System.out.println("Signature: " + Base64.encodeBase64URLSafeString(signature) + "\nPublicKey: " + Base64.encodeBase64URLSafeString(publicKey));
try{
System.out.println("Verification: " + ed25519.checkvalid(signature, "www.example.com".getBytes(), publicKey));
} catch (Exception e){
System.out.println(e.getStackTrace());
}
}
The checkvalid call returns true.
The output signature: oFMU_mC_zzZcJP2C-uTqsyUHoyLUSnwirJbhcdkSTnj2nI_p-VgKAqN5bFMPKsKYiWvyiUgHWu3s4OyB9WbKDg
The output public key: O2onvM62pC1io6jQKm8Nc2UyFXcd4kOmOsBIoYtZ2ik
The javascript code:
var signature = "oFMU_mC_zzZcJP2C-uTqsyUHoyLUSnwirJbhcdkSTnj2nI_p-VgKAqN5bFMPKsKYiWvyiUgHWu3s4OyB9WbKDg";
var pk = "O2onvM62pC1io6jQKm8Nc2UyFXcd4kOmOsBIoYtZ2ik";
var nacl_factory = require('js-nacl');
var nacl = nacl_factory.instantiate();
var b64 = require('urlsafe-base64');
var x = nacl.crypto_sign_open(Uint8Array(b64.decode(signature)), Uint8Array(b64.decode(pk)));
response.send(x);
x
is null, but should give "www.example.com"
as output if the signature could be opened with the public key.
Not sure if it impacts the working, but Java's byte arrays are signed, whereas js-nacl uses Javascript's Uint8Array
unsigned byte arrays.