Dana Chen
Dana Chen

Reputation: 216

TweetNaCl.js Public-key signatures example err

I want to use ed25519 to sign my message. And I found TweetNaCl.js which has the Signatures method to achieve my goal.

By referencing TweetNaCl.js minimal Public-key signatures example and official document-Signatures, here is my test code codesandbox.

  const keyPair = nacl.sign.keyPair();
  const secretKey = keyPair.secretKey;
  const publicKey = keyPair.publicKey;
  const secretKeyB64 = encodeBase64(secretKey);
  const publicKeyB64 = encodeBase64(publicKey);
  console.log("secretKeyB64", secretKeyB64);
  console.log("publicKeyB64", publicKeyB64);

  const msgStr = "My unencrypted message";
  const msg = decodeUTF8(msgStr);
  const signature = nacl.sign(msg, secretKey);
  const signatureB64 = encodeBase64(signature);
  console.log("signatureB64", signatureB64);
  const verifiedMsg = nacl.sign.open(signature, publicKey);
  console.log(encodeUTF8(verifiedMsg));

The encodeUTF8(verifiedMsg)'s console log seems no problem and is as same as msgStr.

But I notice that when I put the publicKeyB64, signatureB64, and msgStr to the tweetnacl.js' example page(Public-key signatures) to verify it, it responds error Bad signature length: must be 64 bytes.

If I put the secretKeyB64 into the example sign page, and click "Sign", the Signature seems shorter than what I created from Codesandbox's code.

Is there anything that I missed?

Upvotes: 2

Views: 1721

Answers (1)

Dana Chen
Dana Chen

Reputation: 216

I figure out this problem 20 minutes later... Thanks for the StackOverflow ?!!

I console log the signature and find its length is over 64. After I reviewed the Signatures, I found another method nacl.sign.detached(message, secretKey) and its length was 64!!!

After encodeBase64 and put it to the tweetnacl.js' example page(Public-key signatures) to verify it, it works!!

Upvotes: 3

Related Questions