Skip to content

Commit

Permalink
working!
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Metcalf committed Nov 15, 2014
1 parent 342c74f commit 130b1e2
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 25 deletions.
14 changes: 9 additions & 5 deletions algos.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
exports['RSA-SHA224'] = exports.sha224WithRSAEncryption = {
sign: 'rsa',
hash: 'sha224'
hash: 'sha224',
id: new Buffer('302d300d06096086480165030402040500041c', 'hex')
};
exports['RSA-SHA256'] = exports.sha256WithRSAEncryption = {
sign: 'rsa',
hash: 'sha256'
hash: 'sha256',
id: new Buffer('3031300d060960864801650304020105000420', 'hex')
};
exports['RSA-SHA384'] = exports.sha384WithRSAEncryption = {
sign: 'rsa',
hash: 'sha384'
hash: 'sha384',
id: new Buffer('3041300d060960864801650304020205000430', 'hex')
};
exports['RSA-SHA512'] = exports.sha512WithRSAEncryption = {
sign: 'rsa',
hash: 'sha512'
};
hash: 'sha512',
id: new Buffer('3051300d060960864801650304020305000440', 'hex')
};
34 changes: 20 additions & 14 deletions inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,65 @@ var verify = require('./verify');
var Writable = require('readable-stream').Writable;
var inherits = require('inherits');
var algos = require('./algos');
'use strict';
module.exports = function (exports, crypto) {
exports.createSign = createSign;
function createSign(algorithm) {
var data = algos[algorithm];
return new Sign(crypto.createHash(data.hash));

return new Sign(algorithm, crypto);
}
exports.createVerify = createVerify;
function createVerify(algorithm) {
var data = algos[algorithm];
return new Verify(crypto.createHash(data.hash));
return new Verify(algorithm, crypto);
}
};
inherits(Sign, Writable);
function Sign(hash) {
Writable.call(this)
this._hash = hash;
function Sign(algorithm, crypto) {
Writable.call(this);
var data = algos[algorithm];
this._hash = crypto.createHash(data.hash);
this._tag = data.id;
};
Sign.prototype._write = function _write(data, _, done) {
this._hash.update(data);
done();
};
Sign.prototype.update = function update(data) {
this.write(data);
return this;
};

Sign.prototype.sign = function sign(key, enc) {
Sign.prototype.sign = function signMethod(key, enc) {
this.end();
var hash = this._hash.digest();
var sig = sign(hash, key);
var sig = sign(Buffer.concat([this._tag, hash]), key);
if (enc) {
sig = sig.toString(enc);
}
return sig;
};

inherits(Verify, Writable);
function Verify(hash) {
Writable.call(this)
this._hash = hash;
function Verify(algorithm, crypto) {
Writable.call(this);
var data = algos[algorithm];
this._hash = crypto.createHash(data.hash);
this._tag = data.id;
};
Verify.prototype._write = function _write(data, _, done) {
this._hash.update(data);
done();
};
Verify.prototype.update = function update(data) {
this.write(data);
return this;
};

Verify.prototype.verify = function verify(key, sig, enc) {
Verify.prototype.verify = function verifyMethod(key, sig, enc) {
this.end();
var hash = this._hash.digest();
if (!Buffer.isBuffer(sig)) {
sig = new Buffer(sig, enc);
}
return verify(sig, hash, key);
return verify(sig, Buffer.concat([this._tag, hash]), key);
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test/index.js | tspec"
},
"author": "",
"license": "ISC",
Expand All @@ -15,5 +15,9 @@
"bn.js": "^0.15.2",
"asn1.js": "^0.6.4",
"readable-stream": "^1.0.33"
},
"devDependencies": {
"tap-spec": "^1.0.1",
"tape": "^3.0.3"
}
}
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ a package to duplicate the functionality of node's crypto public key functions,

# todo

- tests to make sure we actually did it
- ~~tests to make sure we actually did it~~
- chinese remainder theorom?
- eliptical curve signing
- publicEncrypt and privateDecrypt?
- publicEncrypt and privateDecrypt?
- other key encodings (non rss format public keys)
9 changes: 7 additions & 2 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function sign(hash, key) {
hash = new bn(hash).toRed(red);

hash = hash.redPow(priv.privateExponent);

return new Buffer(hash.fromRed().toArray());
var out = new Buffer(hash.fromRed().toArray());
if (out.length < len) {
var prefix = new Buffer(len - out.length);
prefix.fill(0);
out = Buffer.concat([prefix, out], len);
}
return out;
}
31 changes: 31 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var test = require('tape');
var fs = require('fs');
var priv1024 = fs.readFileSync(__dirname + '/rsa.1024.priv');
var pub1024 = fs.readFileSync(__dirname + '/rsa.1024.pub');
var priv2028 = fs.readFileSync(__dirname + '/rsa.2028.priv');
var pub2028 = fs.readFileSync(__dirname + '/rsa.2028.pub');
var nodeCrypto = require('crypto');
var myCrypto = require('../');
function testIt(pub, priv, message, scheme) {
test(message.toString(), function (t) {
t.plan(4);
var mySign = myCrypto.createSign(scheme);
var nodeSign = nodeCrypto.createSign(scheme);
var mySig = mySign.update(message).sign(priv);
var nodeSig = nodeSign.update(message).sign(priv);
t.equals(mySig.length, nodeSig.length, 'correct length');
t.equals(mySig.toString('hex'), nodeSig.toString('hex'), 'equal sigs');
var myVer = myCrypto.createVerify(scheme);
var nodeVer = nodeCrypto.createVerify(scheme);
t.ok(nodeVer.update(message).verify(pub, mySig), 'test node');
t.ok(myVer.update(message).verify(pub, nodeSig), 'test me');
});
}
testIt(pub1024, priv1024, new Buffer('sha224 with 1024 keys'), 'RSA-SHA224');
testIt(pub2028, priv2028, new Buffer('sha224 with 2028 keys'), 'RSA-SHA224');
testIt(pub1024, priv1024, new Buffer('SHA256 with 1024 keys'), 'RSA-SHA256');
testIt(pub2028, priv2028, new Buffer('SHA256 with 2028 keys'), 'RSA-SHA256');
testIt(pub1024, priv1024, new Buffer('SHA384 with 1024 keys'), 'RSA-SHA384');
testIt(pub2028, priv2028, new Buffer('SHA384 with 2028 keys'), 'RSA-SHA384');
testIt(pub1024, priv1024, new Buffer('SHA512 with 1024 keys'), 'RSA-SHA512');
testIt(pub2028, priv2028, new Buffer('SHA512 with 2028 keys'), 'RSA-SHA512');
15 changes: 15 additions & 0 deletions test/rsa.1024.priv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-----BEGIN RSA PRIVATE KEY-----
MIICVAIBAAJ/OwswbFo/uyC8ltGf/yA1A+gV5IGdnAgPbUSI3GzbHCA+x+TLG/tL
vbRw3r1smppY/jkkpiVW1ErSMuN0uixp5gb78Z9rH1XpWb5WWgp3WaY/9EHMjMdO
kQ/9LVZvRvl/M/Fi6owP+q+amJI1BEjECYfbhGL3rmlVdq4qXc40QwIDAQABAn8I
VZ0BPoAOhyF33KFMHxy8r28fsVgxJUYgM3NqQgdv4fFawCYXjhJz9duU5YJGFJGJ
WUGeHlkyYFlpi4f3m7tY7JawmQUWB0MNSoKHI3cgDX4/tfBN8ni+cO0eSoR5czBY
EsAHBU47p1awNFAHwd+ZEuv9H4RmMn7p279rQTtpAkAH3Nqs2/vrRF2cZUN4fIXf
4xHsQBByUayGq8a3J0UGaSFWv68zTUKFherr9uZotNp7NJ4jBXiARw0q8docXUG1
AkAHgmOKHoORtAmikqpmFEJZOtsXMaLCIm4EszPo5ciYoLMBcVit09AdiQlt7ZJL
DY02svU1b0agCZ97kDkmHDkXAkACa8M9JELuDs/P/vIGYDkMVatIFfW6bWF02eFG
taWwMqCcSEsWvbw0xqYt34jURpNbCjmCyQVwYfAw/+TLhP9dAkAFwRjdwjw37qpj
ddg1mNiu37b7swFxmkiMOXZRxaNNsfb56A14RpN3zob3QdGUybGodMIKTFbmU/lu
CjqAxafJAkAG2yf6RWbwFIWfMyt7WYCh0VaGBCcgy574AinVieEo3ZZyFfC63+xm
3uoaNy4iLoJv4GCjqUBz3ZfcVaO/DDWG
-----END RSA PRIVATE KEY-----
5 changes: 5 additions & 0 deletions test/rsa.1024.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN RSA PUBLIC KEY-----
MIGGAn87CzBsWj+7ILyW0Z//IDUD6BXkgZ2cCA9tRIjcbNscID7H5Msb+0u9tHDe
vWyamlj+OSSmJVbUStIy43S6LGnmBvvxn2sfVelZvlZaCndZpj/0QcyMx06RD/0t
Vm9G+X8z8WLqjA/6r5qYkjUESMQJh9uEYveuaVV2ripdzjRDAgMBAAE=
-----END RSA PUBLIC KEY-----
27 changes: 27 additions & 0 deletions test/rsa.2028.priv
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEjwIBAAKB/gy7mjaWgPeFdVYDZWRCA9BNiv3pPb0es27+FKY0hszLaOw47ExC
tAWpDsH48TXAfyHBYwBLguayfk4LGIupxb+CGMbRo3xEp0CbfY1Jby26T9vGjRC1
foHDDUJG84uaRbyHqaf4i6zt4gVR+xlAEIjkaFAAK8cOoXAT1CVqGLLljUCchL8P
jaHj/yriZ/S7rdwlI3LnABxwwmLrmR/v71WtpmO/aNG8N+1po+QwaghTkyQ59E/Z
vAuOkFWHok2q/R6PYAa2jdZ9zim0FqOP+nkQaEDRbBFBmBqTv5fFGfk2WsAfKf/R
G0/VFd+ZeM5251TeTvXH695nlSGauVl9AgMBAAECgf4LrWHY/l54ouThZWvvbrug
pfz6sJX2g9l7yXmWlEWsPECVo/7SUbpYFpt6OZy99zSg+IKbGqWKfdhoKrTwIVtC
L0YZ0NlmdnANSIz0roxQG7ZxkL5+vHSw/PmD9x4Uwf+Cz8hATCmNBv1qc60dkyuW
4CLqe72qaTiVWRoO1iagQghNcLoo6vSy65ExLaCDTPha7yu2vw4hFZpWiEjW4dxf
rFdLiix52BC86YlAlxME/rLg8IJVvilbyo9aWdXmxOaUTLRv6PkFD1/gVdw8V9Qr
SLN9FlK2kkjiX0dzoibvZw3tMnt3yydAx0X87+sMRVahC1bp3kVPz4Hy0EWX4QJ/
PM31vGiuITk2NCd51DXt1Ltn2OP5FaJSmCaEjh0XkU4qouYyjXWt8Bu6BTCl2vua
Fg0Uji9C+IkPLmaUMbMIOwaTk8cWqLthSxsLe70J5OkGrgfKUM/w+BHH1Pt/Pjzj
C++l0kiFaOVDVaAV9GpLPLCBoK/PC9Rb/rxMMoCCNwJ/NZuedIny2w3LMii77h/T
zSvergNGhjY6Rnva8lLXJ6dlrkcPAyps3gWwxqj4NR0T+GM0bDUPVLb7M07XV7SX
v7VJGm52JbRGwM1ss+r8XTTNemeGk+WRxG7TgtsMqYGXLfB8Qxk/f5/Mcc00Tl8u
wXFNsfxJxmt6AbsTr3g36wJ/IhOnibz9Ad+nchlBnN3QeW3CKHqzaR18voqvtVm2
kJfHK15prH/sSGmxmiEGgrCJTZxtDbaNCO7/VBjnKudUUIhCAwsLtuq0/zub9vAd
8G1scfIpv5qaSNzmKoX8bOwArvrS6wP7yKrcTsuWIlHD8rJVI7IEDnQoTp5G8fK1
hwJ/MIh8M5v0r5dUYEv6oIJWGcle6AH1JmsP5WIafgq72Z2288pHcCFHwNY8Dg9J
76QswVLnUhPTlmm3EOOPGEtam2iAD5r0Afytlb4lbNoQsj2szeXONDXB+6oueajh
VNELUr8HcSP5lgzRZjJW6aFIzj9LDRmQnUAOjGSXVOQtEwJ/MCQZ7N/v4dIKeDRA
8d8UExZ3+gGHumziztGRJ0tQryZH2PakP5I7V+1l7qEUnJ2c3mF+e1v41Ep9LCvh
bzrPKw9dxh18g4b+7bMpsWPnsraKh6ipxc7aaOaZV0Dxgez4zcZu0P1olO0cN3KM
nxJ0Pds3R8bAhNCDdS2JZaRp5Q==
-----END RSA PRIVATE KEY-----
8 changes: 8 additions & 0 deletions test/rsa.2028.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-----BEGIN RSA PUBLIC KEY-----
MIIBBgKB/gy7mjaWgPeFdVYDZWRCA9BNiv3pPb0es27+FKY0hszLaOw47ExCtAWp
DsH48TXAfyHBYwBLguayfk4LGIupxb+CGMbRo3xEp0CbfY1Jby26T9vGjRC1foHD
DUJG84uaRbyHqaf4i6zt4gVR+xlAEIjkaFAAK8cOoXAT1CVqGLLljUCchL8PjaHj
/yriZ/S7rdwlI3LnABxwwmLrmR/v71WtpmO/aNG8N+1po+QwaghTkyQ59E/ZvAuO
kFWHok2q/R6PYAa2jdZ9zim0FqOP+nkQaEDRbBFBmBqTv5fFGfk2WsAfKf/RG0/V
Fd+ZeM5251TeTvXH695nlSGauVl9AgMBAAE=
-----END RSA PUBLIC KEY-----
1 change: 0 additions & 1 deletion verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function verify(sig, hash, key) {
sig = sig.redPow(new bn(pub.publicExponent));

sig = new Buffer(sig.fromRed().toArray());
console.log(sig.toString('hex'));
sig = sig.slice(sig.length - hash.length);
var out = 0;
var len = sig.length;
Expand Down

0 comments on commit 130b1e2

Please sign in to comment.