Last active
May 4, 2023 17:53
-
-
Save jeffdonthemic/46a51e310eedbd7b8b64d2b5da2e3e97 to your computer and use it in GitHub Desktop.
Node crypto sign/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 crypto = require('crypto') | |
const fs = require('fs') | |
function sign(plainText) { | |
const sign = crypto.createSign('RSA-SHA512') | |
sign.update(Buffer.from(plainText, 'utf8')) | |
signature = sign.sign( | |
{ | |
key: fs.readFileSync('./salesforce_private.key', 'utf8'), | |
padding:crypto.constants.RSA_PKCS1_PSS_PADDING | |
} | |
).toString('base64') | |
return signature; | |
} | |
function verify(signature, originMsg) { | |
const verify = crypto.createVerify('RSA-SHA512') | |
verify.update(Buffer.from(originMsg, 'utf8')) | |
verifyRes = verify.verify( | |
{ | |
key: fs.readFileSync('./salesforce_public.key', 'utf8'), | |
padding:crypto.constants.RSA_PKCS1_PSS_PADDING}, | |
Buffer.from(signature, 'base64') | |
) | |
return verifyRes; | |
} | |
const plainText = "reports50"; | |
let s = sign(plainText); | |
console.log(s); | |
let results = verify(s, plainText); | |
console.log(results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment