Created
December 29, 2019 13:55
-
-
Save ender503/5eace5041fb07d0d083b2c4d178d6465 to your computer and use it in GitHub Desktop.
node-forge snippets
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 generateKeyPair = ({ bits = 1024, e = 0x10001 } = {}) => { | |
const keypair = forge.rsa.generateKeyPair({ bits, e }); | |
const publicKey = forge.pki.publicKeyToPem(keypair.publicKey); | |
const privateKey = forge.pki.privateKeyToPem(keypair.privateKey); | |
return { | |
publicKey, | |
privateKey, | |
}; | |
}; | |
const createSignature = (message, privateKeyPem) => { | |
const privateKey = forge.pki.privateKeyFromPem(privateKeyPem); | |
const md = forge.md.sha256.create(); | |
md.update(message, 'utf8'); | |
const signature = privateKey.sign(md); | |
return forge.util.encode64(signature); | |
}; | |
const verifySignature = (message, signature, publicKeyPem) => { | |
const publicKey = forge.pki.publicKeyFromPem(publicKeyPem); | |
const md = forge.md.sha256.create(); | |
md.update(message, 'utf8'); | |
return publicKey.verify(md.digest().bytes(), forge.util.decode64(signature)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment