Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata
Created November 8, 2018 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dviejopomata/c87078b9a1170732358cea2ed504d30c to your computer and use it in GitHub Desktop.
Save Dviejopomata/c87078b9a1170732358cea2ed504d30c to your computer and use it in GitHub Desktop.
const crypto = require("crypto")
const fs = require("fs")
const sign = crypto.createSign("SHA256")
const text = "hello world"
// openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
const privateKey = fs.readFileSync("./key.pem")
const publicKey = fs.readFileSync("./cert.pem")
const passphrase = "123456"
sign.update(text)
sign.end()
const signature = sign.sign({ key: privateKey, passphrase }, "hex")
// verify
const verify = crypto.createVerify("SHA256")
verify.update(text)
const ok = verify.verify(publicKey, signature, "hex")
console.log(ok)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment