Last active
June 29, 2022 19:10
-
-
Save mirismaili/ba258b86b177590cfd78d9fa4bfcf3f3 to your computer and use it in GitHub Desktop.
Sign (using private key) and Verify (using public key) in node.js #cryptography
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
import {createPrivateKey, generateKeyPairSync, sign, verify} from 'node:crypto' | |
const passphrase = 'passphrase' | |
const {publicKey, privateKey} = generateKeyPairSync('ed25519', { | |
privateKeyEncoding: {format: 'pem', type: 'pkcs8', cipher: 'aes-256-cbc', passphrase}, | |
publicKeyEncoding: {format: 'pem', type: 'spki'}, | |
}) | |
console.log(privateKey, publicKey) | |
const message = 'Hello world!' | |
console.log({message}) | |
const signature = sign(null, Buffer.from(message), createPrivateKey({key: privateKey, passphrase})) | |
console.log('signature:', signature.toString('hex')) | |
const verified = verify(null, Buffer.from(message), publicKey, signature) | |
console.log({verified}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment