Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// make sure the required parameters are provided
if (!(signingInputHash && rawPrivateKey)) {
throw new MissingParametersError(
'a signing input hash and private key are all required')
}
// prepare the private key
const privateKeyObject = SECP256K1Client.loadPrivateKey(rawPrivateKey)
// calculate the signature
const signatureObject = privateKeyObject.sign(signingInputHash)
const derSignature = Buffer.from(signatureObject.toDER())
if (format === 'der') {
return derSignature.toString('hex')
} else if (format === 'jose') {
// return the JOSE-formatted signature
return derToJose(derSignature, 'ES256')
} else {
throw Error('Invalid signature format')
}
}
var signature;
var cryptoAlgName = enums.algCryptoMap[algorithm];
var signingType = enums.algTypeMap[algorithm];
if (!cryptoAlgName) {
throw new errors.UnsupportedSigningAlgorithmJwtError();
}
if (signingType === 'hmac') {
buffer = crypto.createHmac(cryptoAlgName, cryptoInput).update(payload).digest();
} else {
buffer = crypto.createSign(cryptoAlgName).update(payload).sign(cryptoInput);
}
if (helpers.isECDSA(algorithm)) {
signature = ecdsaSigFormatter.derToJose(buffer, algorithm);
} else {
signature = helpers.base64urlEncode(buffer);
}
return signature;
};
return function sign() {
var signature = inner.apply(null, arguments);
signature = formatEcdsa.derToJose(signature, 'ES' + bits);
return signature;
};
}
return function sign() {
var signature = inner.apply(null, arguments);
signature = formatEcdsa.derToJose(signature, 'ES' + bits);
return signature;
};
}