Skip to content

Instantly share code, notes, and snippets.

@mirismaili
Last active June 29, 2022 19:10
Show Gist options
  • Save mirismaili/ba258b86b177590cfd78d9fa4bfcf3f3 to your computer and use it in GitHub Desktop.
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
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