Skip to content

Commit

Permalink
in prog
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Nov 14, 2014
0 parents commit d55225f
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
16 changes: 16 additions & 0 deletions algos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
exports['RSA-SHA224'] = exports.sha224WithRSAEncryption = {
sign: 'rsa',
hash: 'sha224'
};
exports['RSA-SHA256'] = exports.sha256WithRSAEncryption = {
sign: 'rsa',
hash: 'sha256'
};
exports['RSA-SHA384'] = exports.sha384WithRSAEncryption = {
sign: 'rsa',
hash: 'sha384'
};
exports['RSA-SHA512'] = exports.sha512WithRSAEncryption = {
sign: 'rsa',
hash: 'sha512'
};
65 changes: 65 additions & 0 deletions asn1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// from https://github.com/indutny/self-signed/blob/gh-pages/lib/asn1.js
// Fedor, you are amazing.

var asn1 = require('asn1.js');
var rfc3280 = require('asn1.js-rfc3280');

var RSAPrivateKey = asn1.define('RSAPrivateKey', function() {
this.seq().obj(
this.key('version').int(),
this.key('modulus').int(),
this.key('publicExponent').int(),
this.key('privateExponent').int(),
this.key('prime1').int(),
this.key('prime2').int(),
this.key('exponent1').int(),
this.key('exponent2').int(),
this.key('coefficient').int()
);
});
exports.RSAPrivateKey = RSAPrivateKey;

var RSAPublicKey = asn1.define('RSAPublicKey', function() {
this.seq().obj(
this.key('modulus').int(),
this.key('publicExponent').int()
);
});
exports.RSAPublicKey = RSAPublicKey;

var GeneralName = asn1.define('GeneralName', function() {
this.choice({
dNSName: this.implicit(2).ia5str()
});
});
exports.GeneralName = GeneralName;

var GeneralNames = asn1.define('GeneralNames', function() {
this.seqof(GeneralName);
});
exports.GeneralNames = GeneralNames;

var Signature = asn1.define('Signature', function() {
this.seq().obj(
this.key('algorithm').seq().obj(
this.key('algorithm').objid(),
this.null_()
),
this.key('digest').octstr()
);
});
exports.Signature = Signature;

var IA5Str = asn1.define('IA5Str', function() {
this.ia5str();
});
exports.IA5Str = IA5Str;

exports.SHA256 = [ 2, 16, 840, 1, 101, 3, 4, 2, 1 ];
exports.SHA256RSA = [ 1, 2, 840, 113549, 1, 1, 11 ];
exports.RSA = [ 1, 2, 840, 113549, 1, 1, 1 ];
exports.COMMONNAME = [ 2, 5, 4, 3 ];
exports.ALTNAME = [ 2, 5, 29, 17 ];

exports.TBSCertificate = rfc3280.TBSCertificate;
exports.Certificate = rfc3280.Certificate;
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var sign = require('./sign');
var Writable = require('readable-stream').Writable;
var inherits = require('inherits');
exports.createSign = createSign;
function createSign(algorithm) {

}
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "browserify-sign",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"pemstrip": "0.0.1",
"asn1.js-rfc3280": "^0.5.1",
"inherits": "^2.0.1",
"bn.js": "^0.15.2",
"asn1.js": "^0.6.4",
"readable-stream": "^1.0.33"
}
}
25 changes: 25 additions & 0 deletions sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
var pemstrip = require('pemstrip');
var asn1 = require('./asn1');
var bn = require('bn.js');
module.exports = sign;
function sign(hash, key) {
var priv = asn1.RSAPrivateKey.decode(new Buffer(pemstrip.strip(key).base64, 'base64'), 'der');
var len = priv.modulus.byteLength();
var pad = [ 0, 1 ];
while (hash.length + pad.length + 1 < len) {
pad.push(0xff);
}
pad.push(0x00);
var i = -1;
while (++i < hash.length) {
pad.push(hash[i]);
}
hash = pad;
var red = bn.mont(priv.modulus);
hash = new bn(hash).toRed(red);

hash = hash.redPow(priv.privateExponent);

return new Buffer(hash.fromRed().toArray());
}
25 changes: 25 additions & 0 deletions verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
var pemstrip = require('pemstrip');
var asn1 = require('./asn1');
var bn = require('bn.js');
module.exports = verify;
function verify(sig, hash, key) {
var pub = asn1.RSAPublicKey.decode(new Buffer(pemstrip.strip(key).base64, 'base64'), 'der');


var red = bn.mont(pub.modulus);
sig = new bn(sig).toRed(red);

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;
var i = -1;
while (++i < len) {
out += (sig[i] ^ hash[i]);
}
return !out;
}

0 comments on commit d55225f

Please sign in to comment.