Skip to content

Commit

Permalink
add md5 and ripemd160 also make sure to include algorythm id when ver…
Browse files Browse the repository at this point in the history
…ifying
  • Loading branch information
calvinmetcalf committed Nov 25, 2014
1 parent 1a3e3f3 commit da03fb0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
16 changes: 13 additions & 3 deletions algos.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ exports['RSA-SHA1'] = {
sign: 'rsa',
hash: 'sha1',
id: new Buffer('3021300906052b0e03021a05000414', 'hex')
}
};
exports['ecdsa-with-SHA1'] = {
sign: 'ecdsa',
hash: 'sha1',
id: new Buffer(/*'3021300906052b0e03021a05000414'*/'', 'hex')
}
id: new Buffer('', 'hex')
};
exports['RSA-RIPEMD160'] = exports.ripemd160WithRSA = {
sign: 'rsa',
hash: 'rmd160',
id: new Buffer('3021300906052b2403020105000414', 'hex')
};
exports['RSA-MD5'] = exports.md5WithRSAEncryption = {
sign: 'rsa',
hash: 'md5',
id: new Buffer('3020300c06082a864886f70d020505000410', 'hex')
};
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ function ectestIt(keys, message, scheme) {
t.ok(myVer.update(message).verify(pub, nodeSig), 'me validate node sig');
});
}
testIt(rsa1024, new Buffer('md5 with 1024 keys'), 'RSA-MD5');
ectestIt(ec, new Buffer('ecdsa with sha1'), 'ecdsa-with-SHA1');

testIt(rsa1024, new Buffer('md5 with 1024 keys'), 'RSA-MD5');
testIt(rsa2028, new Buffer('md5 with 2028 keys'), 'RSA-MD5');
testIt(nonrsa1024, new Buffer('md5 with 1024 keys non-rsa key'), 'RSA-MD5');
testIt(rsa1024, new Buffer('rmd160 with 2028 keys'), 'RSA-RIPEMD160');
testIt(rsa2028, new Buffer('rmd160 with 1024 keys'), 'RSA-RIPEMD160');
testIt(nonrsa1024, new Buffer('rmd160 with 1024 keys non-rsa key'), 'RSA-RIPEMD160');
testIt(rsa1024, new Buffer('sha1 with 1024 keys'), 'RSA-SHA1');
testIt(rsa2028, new Buffer('sha1 with 2028 keys'), 'RSA-SHA1');
testIt(nonrsa1024, new Buffer('sha1 with 1024 keys non-rsa key'), 'RSA-SHA1');
Expand All @@ -94,4 +100,6 @@ if (!isNode10()) {
testIt(pass1024, new Buffer('sha256 with 1024 keys and password'), 'RSA-SHA256');
testIt(pass1024, new Buffer('sha384 with 1024 keys and password'), 'RSA-SHA384');
testIt(pass1024, new Buffer('sha512 with 1024 keys and password'), 'RSA-SHA512');
testIt(pass1024, new Buffer('rmd160 with 1024 keys and password'), 'RSA-RIPEMD160');
testIt(pass1024, new Buffer('md5 with 1024 keys and password'), 'RSA-MD5');
}
15 changes: 13 additions & 2 deletions verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ function verify(sig, hash, key) {
if (pub.type === 'ec') {
return ecVerify(sig, hash, pub);
}
var len = pub.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]);
}
pad = hash;
var red = bn.mont(pub.modulus);
sig = new bn(sig).toRed(red);

Expand All @@ -16,8 +27,8 @@ function verify(sig, hash, key) {
sig = new Buffer(sig.fromRed().toArray());
sig = sig.slice(sig.length - hash.length);
var out = 0;
var len = sig.length;
var i = -1;
len = sig.length;
i = -1;
while (++i < len) {
out += (sig[i] ^ hash[i]);
}
Expand Down

0 comments on commit da03fb0

Please sign in to comment.