Reputation: 426
I wanna generate an ECDSA signature and convert it to base64.
I can easily accomplish this with Nodejs:
const sign = crypto.createSign('SHA256')
sign.update('part1')
sign.update('part2')
const signature = sign.sign(privateKey, 'base64')
However, when I try to do the same with Javascript SubtleCrypto API, The generated signature is not valid when validated with Node Crypto. Here is my code:
let signature = await window.crypto.subtle.sign(
{
name: "ECDSA",
hash: { name: "SHA-256" },
},
this.privateKey,
"part1"+"part2"
);
signature = new Uint8Array(signature);
const byteArray = new Uint8Array(signature);
let byteString = "";
for (let i = 0; i < byteArray.byteLength; i += 1) {
byteString += String.fromCharCode(byteArray[i]);
}
const b64Signature = window.btoa(byteString);
What Am I missing?
Upvotes: 0
Views: 990
Reputation: 799
The difference is in signature encoding format. See ECDSA signatures between Node.js and WebCrypto appear to be incompatible?
Here are some examples of how you can convert WebCrypto signature to NodeJS and back
https://github.com/PeculiarVentures/webcrypto/blob/master/src/mechs/ec/crypto.ts#L62-L69 https://github.com/PeculiarVentures/webcrypto/blob/master/src/mechs/ec/crypto.ts#L84-L89
Upvotes: 1