My code:
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for the sign operation.
*/
function getMessageEncoding() {
const messageBox = 'hello world!';
let message = messageBox.value;
let enc = new TextEncoder();
return enc.encode(message);
}
async function main() {
let encoded = getMessageEncoding();
window.crypto.subtle.sign(
{
name: "ECDSA",
hash: { name: "SHA-384" },
},
{"alg":"ES256","crv":"P-256","d":"KRiXxoIFHmBQJdoCsqB8Bc9_f2Z-QCdRgoydMKdoL04","ext":true,"key_ops":["sign"],"kty":"EC","x":"FKwqyGd4i2NAl8RUXCCBRCAIbcpeGyfyXwgA_AWHb8Y","y":"njxhw5O6zGVkBlcPDKYj0E-6VO1giHTUkJWBhgKNqd8"},
encoded,
).then(signature => {
console.log(signature);
alert('test');
});
alert(signature);
}
main();
(the private key is a dummy private key and not a production one)
When I run it it doesn't do anything. I'd expect it to put the signature into the JS console?
Here it is on JS Fiddle:
https://jsfiddle.net/pwuersfL/1/
Any ideas?
In the posted code the key import with
importKey()is missing.importKey()converts the JWK into aCryptoKeywhich can be processed bysign().In the following code the missing import is added (plus verifying):
The code produces for P-256 a 64 bytes signature in IEEE P1363 format (r|s), which can be successfully verified with the public key.