Last active
November 12, 2016 22:11
-
-
Save MaxGraey/b4143413fe1c59b9b333f1f374640176 to your computer and use it in GitHub Desktop.
Electron signing in node.js
This file contains hidden or 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
// 1. Generate new private RSA Key: | |
// openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem | |
// 2. Derivate public RSA Key from private: | |
// openssl rsa -in key.pem -out public_key.pem -outform PEM -pubout | |
const crypto = require('crypto'); | |
const fs = require('fs'); | |
const hashes = crypto.getHashes(); | |
//console.log(hashes); | |
var data = 'abcdef'; | |
var privateKey = fs.readFileSync(__dirname + '/key.pem', 'ascii'); | |
var sign = crypto.createSign('RSA-SHA256'); | |
sign.update(data); | |
var signature = sign.sign(privateKey, 'base64'); | |
console.log(signature); | |
var publicKey = fs.readFileSync(__dirname + '/public_key.pem', 'ascii'); | |
var verifier = crypto.createVerify('RSA-SHA256'); | |
verifier.update(data); | |
var isValid = verifier.verify(publicKey, signature, 'base64'); | |
console.log('sign valide: ', isValid); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment