Accept private key too when verify
This commit is contained in:
parent
09f6524f1b
commit
bc3e660761
|
@ -26,8 +26,8 @@ exports.sign = function(privateKey, msg) {
|
|||
}
|
||||
};
|
||||
|
||||
exports.verify = function(publicKey, msg, sig) {
|
||||
var key = ec.keyPair(null, publicKey);
|
||||
exports.verify = function(key, msg, sig) {
|
||||
key = ec.keyPair(key);
|
||||
if (key.verify(msg, sig)) {
|
||||
return Promise.resolve();
|
||||
} else {
|
||||
|
|
8
index.js
8
index.js
|
@ -13,9 +13,10 @@ var secp256k1 = require("secp256k1");
|
|||
* @param {Buffer} publicKey A 32-byte private key
|
||||
* @return {Buffer} A 65-byte public key
|
||||
*/
|
||||
exports.getPublic = function(privateKey) {
|
||||
function getPublic(privateKey) {
|
||||
return secp256k1.createPublicKey(privateKey);
|
||||
};
|
||||
exports.getPublic = getPublic;
|
||||
|
||||
/**
|
||||
* Create an ECDSA signature.
|
||||
|
@ -43,13 +44,14 @@ exports.sign = function(privateKey, msg) {
|
|||
|
||||
/**
|
||||
* Verify an ECDSA signature.
|
||||
* @param {Buffer} publicKey The public key
|
||||
* @param {Buffer} key Private or public key
|
||||
* @param {Buffer} msg The message being verified
|
||||
* @param {Buffer} sig The signature
|
||||
* @return {Promise} A promise that resolves on correct signature and
|
||||
* rejects on bad signature/public key.
|
||||
*/
|
||||
exports.verify = function(publicKey, msg, sig) {
|
||||
exports.verify = function(key, msg, sig) {
|
||||
var publicKey = key.length === 32 ? getPublic(key) : key;
|
||||
return new Promise(function(resolve, reject) {
|
||||
secp256k1.verify(publicKey, msg, sig, function(code) {
|
||||
if (code === 1) {
|
||||
|
|
16
test.js
16
test.js
|
@ -6,12 +6,17 @@ var privateKey = Buffer(32);
|
|||
privateKey.fill(1);
|
||||
var publicKey = eccrypto.getPublic(privateKey);
|
||||
var msg = crypto.createHash("sha256").update("test").digest();
|
||||
var otherMsg = crypto.createHash("sha256").update("test2").digest();
|
||||
|
||||
describe("Key", function() {
|
||||
describe("Key convertion", function() {
|
||||
it("should allow to convert private key to public", function() {
|
||||
expect(Buffer.isBuffer(publicKey)).to.be.true;
|
||||
expect(publicKey.toString("hex")).to.equal("041b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f70beaf8f588b541507fed6a642c5ab42dfdf8120a7f639de5122d47a69a8e8d1");
|
||||
});
|
||||
|
||||
it("should throw on invalid private key", function() {
|
||||
expect(eccrypto.getPublic.bind(null, Buffer("test"))).to.throw(Error);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ECDSA", function() {
|
||||
|
@ -24,11 +29,18 @@ describe("ECDSA", function() {
|
|||
});
|
||||
});
|
||||
|
||||
it("should allow to verify using private key", function() {
|
||||
return eccrypto.sign(privateKey, msg)
|
||||
.then(function(sig) {
|
||||
expect(Buffer.isBuffer(sig)).to.be.true;
|
||||
return eccrypto.verify(privateKey, msg, sig);
|
||||
});
|
||||
});
|
||||
it("shouldn't verify incorrect signature", function(done) {
|
||||
eccrypto.sign(privateKey, msg)
|
||||
.then(function(sig) {
|
||||
expect(Buffer.isBuffer(sig)).to.be.true;
|
||||
return eccrypto.verify(publicKey, Buffer("other msg"), sig);
|
||||
return eccrypto.verify(publicKey, otherMsg, sig);
|
||||
}).catch(function() {
|
||||
done();
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user