diff --git a/package.json b/package.json index 76217cd..7363f6d 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/sign.js b/sign.js index a5f123e..891c098 100644 --- a/sign.js +++ b/sign.js @@ -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); @@ -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;