Skip to content

Instantly share code, notes, and snippets.

@heapwolf heapwolf/crypto.js
Created Oct 3, 2012

Embed
What would you like to do?
var crypto = require('crypto');
var path = require('path');
var fs = require('fs');
var key = fs.readFileSync(path.join(process.env['HOME'], '.ssh/foo_rsa')).toString();
var pubkey = fs.readFileSync(path.join(process.env['HOME'], '.ssh/foo_rsa.pub')).toString();
var data = "abcdef"
var sign = crypto.createSign('RSA-SHA256');
sign.update(data);
var Sig = sign.sign(key, 'hex');
var verify = crypto.createVerify('RSA-SHA256');
verify.update(data);
console.log(verify.verify(pubkey, Sig, 'hex'));
@heapwolf

This comment has been minimized.

Copy link
Owner Author

commented Oct 3, 2012

$openssl genrsa -out foo_rsa 256
Generating RSA private key, 256 bit long modulus
......+++++++++++++++++++++++++++
...+++++++++++++++++++++++++++
e is 65537 (0x10001)
$openssl rsa -in foo_rsa -pubout -out foo_rsa.pub
writing RSA key
@heapwolf

This comment has been minimized.

Copy link
Owner Author

commented Oct 3, 2012

The problem is that the verify method always returns false.

@heapwolf

This comment has been minimized.

Copy link
Owner Author

commented Oct 3, 2012

$node --version
v0.9.0

$npm --version
1.1.44
@heapwolf

This comment has been minimized.

Copy link
Owner Author

commented Oct 3, 2012

Solution...

# generate a private key to use with your service
$openssl genrsa -out nodejitsu_rsa 1024
# create a public key to share with the service
$openssl rsa -in nodejitsu_rsa -pubout > nodejitsu_rsa.pub
var crypto = require('crypto');
var path = require('path');
var fs = require('fs');

var key = fs.readFileSync(path.join(process.env['HOME'], '.ssh/nodejitsu_rsa')).toString('ascii');
var pubkey = fs.readFileSync(path.join(process.env['HOME'], '.ssh/nodejitsu_rsa.pub')).toString('ascii');

var data = "abcdef";

var sign = crypto.createSign('SHA256');
sign.update(data);

var Sig = sign.sign(key, 'base64');

console.log(Sig);

var verify = crypto.createVerify('SHA256');
verify.update(data);

console.log(verify.verify(pubkey, Sig, 'base64'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.