Skip to content

Commit 240e16c

Browse files
committed
left pad here and clean up tests
1 parent ceb731f commit 240e16c

File tree

5 files changed

+81
-13
lines changed

5 files changed

+81
-13
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- "0.11"

index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
var bn = require('bn.js');
22
module.exports = crt;
3-
// based on https://github.com/google/end-to-end/blob/bd14d9607e742cd94b1a5af39e0f9e8c454b4a32/src/javascript/crypto/e2e/asymmetric/rsa.js#L196
43
function blind(priv, crypto) {
5-
var mod = bn.mont(priv.modulus);
64
var r = getr(priv, crypto);
7-
var p = priv.prime1;
8-
var q = priv.prime2;
9-
var ONE = new bn(1);
10-
11-
var blinder = r.toRed(mod)
5+
var blinder = r.toRed(bn.mont(priv.modulus))
126
.redPow(new bn(priv.publicExponent)).fromRed();
137
return {
148
blinder: blinder,
@@ -17,6 +11,7 @@ function blind(priv, crypto) {
1711
}
1812
function crt(msg, priv, crypto) {
1913
var blinds = blind(priv, crypto);
14+
var len = priv.modulus.byteLength();
2015
var mod = bn.mont(priv.modulus);
2116
var blinded = new bn(msg).mul(blinds.blinder).mod(priv.modulus);
2217
var c1 = blinded.toRed(bn.mont(priv.prime1));
@@ -31,9 +26,15 @@ function crt(msg, priv, crypto) {
3126
var h = m1.isub(m2).imul(qinv).mod(p);
3227
h.imul(q);
3328
m2.iadd(h);
34-
return new Buffer(m2.imul(blinds.unblinder).mod(priv.modulus).toArray());
29+
var out = new Buffer(m2.imul(blinds.unblinder).mod(priv.modulus).toArray());
30+
if (out.length < len) {
31+
var prefix = new Buffer(len - out.length);
32+
prefix.fill(0);
33+
out = Buffer.concat([prefix, out], len);
34+
}
35+
return out;
3536
}
36-
37+
crt.getr = getr;
3738
function getr(priv, crypto) {
3839
var len = priv.modulus.byteLength();
3940
var r = new bn(crypto.randomBytes(len));

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "browserify-rsa",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "node test.js | tspec"
88
},
99
"author": "",
1010
"license": "MIT",
@@ -14,5 +14,10 @@
1414
"repository": {
1515
"type": "git",
1616
"url": "[email protected]:calvinmetcalf/browserify-rsa.git"
17+
},
18+
"devDependencies": {
19+
"tap-spec": "^2.1.2",
20+
"tape": "^3.0.3",
21+
"parse-asn1": "^1.2.0"
1722
}
18-
}
23+
}

readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
browserify-rsa
22
====
3+
[![Build Status](https://travis-ci.org/calvinmetcalf/browserify-rsa.svg)](https://travis-ci.org/calvinmetcalf/browserify-rsa)
34

4-
RSA private decryption/signing using chinese remainder and blinding.
5+
RSA private decryption/signing using chinese remainder and blinding.
6+
7+
API
8+
====
9+
10+
Give it a message as a buffer, a private key (as decoded by https://www.npmjs.com/package/parse-asn1) and a crypto object (aka `require('crypto')`, this is because we use it in browserify crypto and don't want to create a circular dependency)

test.js

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)