Last active
May 30, 2021 05:12
-
-
Save meirkl/0bd06afdac3542b23abbe7f0844cd4a8 to your computer and use it in GitHub Desktop.
Node JS EC verify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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