I am trying to generate key pairs using SubtleCrypto API in javascript and then using the same to sign some data using ECDSA algorithm. Documentation for the same can be found at this link.
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign
An implementation of the same API can be found at this link.
https://github.com/diafygi/webcrypto-examples/
Now the problem I am facing is the signature produced by the API is different for the same private key and data, which should never be the case.
var gprivateKeyobject = null;
var gpublicKeyobject = null;
var gsignature =null;
window.crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-256",
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["sign", "verify"] //can be any combination of "sign" and "verify"
)
.then(function(key){
//returns a keypair object
gprivateKeyobject = key.privateKey;
gpublicKeyobject = key.publicKey;
console.log('keys generated');
})
.catch(function(err){
console.error(err);
});
var str = "hello test 1233";
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++)
{
bufView[i] = str.charCodeAt(i);
}
console.log(bufView);
function sign()
{
window.crypto.subtle.sign(
{
name: "ECDSA",
hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
gprivateKeyobject, //from generateKey or importKey above
bufView //ArrayBuffer of data you want to sign
)
.then(function(signature){
//returns an ArrayBuffer containing the signature
//console.log(bufView);
console.log(signature);
console.log(String.fromCharCode.apply(null, new Uint8Array(signature)));
})
.catch(function(err){
console.error(err);
});
}
my console showing different signatures produced at consecutive calls to the function sign(which implies that private key and data to be signed must not have changed)
