Skip to content

Commit

Permalink
pull out rsa stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Dec 22, 2014
1 parent 8a96544 commit 0c076ff
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"license": "ISC",
"dependencies": {
"bn.js": "^0.15.2",
"browserify-rsa": "^1.1.0",
"elliptic": "^0.15.14",
"inherits": "^2.0.1",
"parse-asn1": "^1.2.0"
Expand Down
23 changes: 2 additions & 21 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var parseKeys = require('parse-asn1');
var bn = require('bn.js');
var elliptic = require('elliptic');
var crt = require("browserify-rsa");
module.exports = sign;
function sign(hash, key, crypto) {
var priv = parseKeys(key, crypto);
Expand All @@ -21,29 +22,9 @@ function sign(hash, key, crypto) {
pad.push(hash[i]);
}

var out = crt(pad, priv);
if (out.length < len) {
var prefix = new Buffer(len - out.length);
prefix.fill(0);
out = Buffer.concat([prefix, out], len);
}
var out = crt(pad, priv, crypto);
return out;
}
function crt(msg, priv) {
var c1 = new bn(msg).toRed(bn.mont(priv.prime1));
var c2 = new bn(msg).toRed(bn.mont(priv.prime2));
var qinv = new bn(priv.coefficient);
var p = new bn(priv.prime1);
var q = new bn(priv.prime2);
var m1 = c1.redPow(priv.exponent1);
var m2 = c2.redPow(priv.exponent2);
m1 = m1.fromRed();
m2 = m2.fromRed();
var h = m1.isub(m2).imul(qinv).mod(p);
h.imul(q);
m2.iadd(h);
return new Buffer(m2.toArray());
}
function ecSign(hash, priv, crypto) {
elliptic.rand = crypto.randomBytes;
var curve;
Expand Down

0 comments on commit 0c076ff

Please sign in to comment.