Skip to content

Instantly share code, notes, and snippets.

@meirkl
Last active May 30, 2021 05:12
Show Gist options
  • Save meirkl/0bd06afdac3542b23abbe7f0844cd4a8 to your computer and use it in GitHub Desktop.
Save meirkl/0bd06afdac3542b23abbe7f0844cd4a8 to your computer and use it in GitHub Desktop.
Node JS EC verify
const { generateKeyPairSync, createSign, createVerify } = require('crypto');
const ALGORITHM = 'ec';
const NAMED_CURVE = 'P-256';
const HASH_FUNCTION = 'sha256';
const ENCODING = 'base64';
const { privateKey, publicKey } = generateKeyPairSync(ALGORITHM, {
namedCurve: NAMED_CURVE,
});
const message = 'Hello 🌍';
// sign
const sign = createSign(HASH_FUNCTION);
sign.update(message);
const signature = sign.sign(privateKey, ENCODING);
// verify
const verify = createVerify(HASH_FUNCTION);
verify.update(message + '😈');
if (verify.verify(publicKey, signature, ENCODING)) {
console.log('Verified OK');
} else {
console.log('Verification Failure');
}
const { generateKeyPairSync, createSign, createVerify } = require('crypto');
const ALGORITHM = 'ec';
const NAMED_CURVE = 'P-256';
const HASH_FUNCTION = 'sha256';
const ENCODING = 'base64';
const { privateKey, publicKey } = generateKeyPairSync(ALGORITHM, {
namedCurve: NAMED_CURVE,
});
const message = 'Hello 🌍';
// sign
const sign = createSign(HASH_FUNCTION);
sign.update(message);
const signature = sign.sign(privateKey, ENCODING);
// verify
const verify = createVerify(HASH_FUNCTION);
verify.update(message);
if (verify.verify(publicKey, signature, ENCODING)) {
console.log('Verified OK');
} else {
console.log('Verification Failure');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment