Update for elliptic 2.0

This commit is contained in:
Kagami Hiiragi 2015-01-18 23:23:16 +03:00
parent 4bce776eb4
commit 98a0913340
3 changed files with 13 additions and 10 deletions

View File

@ -67,7 +67,7 @@ var getPublic = exports.getPublic = function(privateKey) {
assert(privateKey.length === 32, "Bad private key"); assert(privateKey.length === 32, "Bad private key");
// XXX(Kagami): `elliptic.utils.encode` returns array for every // XXX(Kagami): `elliptic.utils.encode` returns array for every
// encoding except `hex`. // encoding except `hex`.
return new Buffer(ec.keyPair(privateKey).getPublic("arr")); return new Buffer(ec.keyFromPrivate(privateKey).getPublic("arr"));
}; };
// NOTE(Kagami): We don't use promise shim in Browser implementation // NOTE(Kagami): We don't use promise shim in Browser implementation
@ -78,8 +78,7 @@ var getPublic = exports.getPublic = function(privateKey) {
exports.sign = function(privateKey, msg) { exports.sign = function(privateKey, msg) {
return new Promise(function(resolve) { return new Promise(function(resolve) {
assert(privateKey.length === 32, "Bad private key"); assert(privateKey.length === 32, "Bad private key");
var key = ec.keyPair(privateKey); resolve(new Buffer(ec.sign(msg, privateKey).toDER()));
resolve(new Buffer(key.sign(msg).toDER()));
}); });
}; };
@ -87,8 +86,7 @@ exports.verify = function(publicKey, msg, sig) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
assert(publicKey.length === 65, "Bad public key"); assert(publicKey.length === 65, "Bad public key");
assert(publicKey[0] === 4, "Bad public key"); assert(publicKey[0] === 4, "Bad public key");
var key = ec.keyPair(publicKey); return ec.verify(msg, sig, publicKey) ? resolve() : reject();
return key.verify(msg, sig) ? resolve() : reject();
}); });
}; };
@ -97,8 +95,8 @@ var derive = exports.derive = function(privateKeyA, publicKeyB) {
assert(privateKeyA.length === 32, "Bad private key"); assert(privateKeyA.length === 32, "Bad private key");
assert(publicKeyB.length === 65, "Bad public key"); assert(publicKeyB.length === 65, "Bad public key");
assert(publicKeyB[0] === 4, "Bad public key"); assert(publicKeyB[0] === 4, "Bad public key");
var keyA = ec.keyPair(privateKeyA); var keyA = ec.keyFromPrivate(privateKeyA);
var keyB = ec.keyPair(publicKeyB); var keyB = ec.keyFromPublic(publicKeyB);
var Px = keyA.derive(keyB.getPublic()); // BN instance var Px = keyA.derive(keyB.getPublic()); // BN instance
resolve(new Buffer(Px.toString(16, 2), "hex")); resolve(new Buffer(Px.toString(16, 2), "hex"));
}); });

View File

@ -49,7 +49,7 @@
"mocha": "*" "mocha": "*"
}, },
"dependencies": { "dependencies": {
"elliptic": "^1.0.1", "elliptic": "^2.0.0",
"es6-promise": "^2.0.1", "es6-promise": "^2.0.1",
"nan": "^1.4.1" "nan": "^1.4.1"
}, },

View File

@ -64,11 +64,16 @@ describe("ECDSA", function() {
it("should reject promise on invalid key when verifying", function(done) { it("should reject promise on invalid key when verifying", function(done) {
eccrypto.sign(privateKey, msg).then(function(sig) { eccrypto.sign(privateKey, msg).then(function(sig) {
expect(Buffer.isBuffer(sig)).to.be.true; expect(Buffer.isBuffer(sig)).to.be.true;
eccrypto.verify(Buffer("test"), msg, sig).catch(function(e) { eccrypto.verify(Buffer("test"), msg, sig).catch(function() {
var badKey = new Buffer(65);
publicKey.copy(badKey);
badKey[0] ^= 1;
eccrypto.verify(badKey, msg, sig).catch(function() {
done(); done();
}); });
}); });
}); });
});
it("should reject promise on invalid sig when verifying", function(done) { it("should reject promise on invalid sig when verifying", function(done) {
eccrypto.sign(privateKey, msg).then(function(sig) { eccrypto.sign(privateKey, msg).then(function(sig) {