This commit is contained in:
Kagami Hiiragi 2015-01-13 15:11:53 +03:00
parent 9a9555a797
commit 4e3f857332
3 changed files with 15 additions and 14 deletions

View File

@ -38,6 +38,8 @@ So we use [seck256k1](https://www.npmjs.com/package/secp256k1) library in Node f
## Usage ## Usage
### ECDSA
```js ```js
var crypto = require("crypto"); var crypto = require("crypto");
var eccrypto = require("eccrypto"); var eccrypto = require("eccrypto");
@ -45,19 +47,27 @@ var eccrypto = require("eccrypto");
var privateKey = crypto.randomBytes(32); var privateKey = crypto.randomBytes(32);
var publicKey = eccrypto.getPublic(privateKey); var publicKey = eccrypto.getPublic(privateKey);
var str = "msg to sign"; var str = "msg to sign";
// Always hash you msg to sign! // Always hash you message to sign!
var msg = crypto.createHash("sha256").update(str).digest(); var msg = crypto.createHash("sha256").update(str).digest();
eccrypto.sign(privateKey, msg).then(function(sig) { eccrypto.sign(privateKey, msg).then(function(sig) {
console.log("signed:", sig); console.log("signed:", sig);
// Public key is sufficient for verifying but private key also could be
// passed for convinience.
eccrypto.verify(publicKey, msg, sig).then(function() { eccrypto.verify(publicKey, msg, sig).then(function() {
console.log("verified"); console.log("verified");
}); });
}); });
``` ```
### ECDH
```js
```
### ECIES
```js
```
## License ## License
eccrypto - JavaScript Elliptic curve cryptography library eccrypto - JavaScript Elliptic curve cryptography library

View File

@ -33,14 +33,13 @@ exports.sign = function(privateKey, msg) {
/** /**
* Verify an ECDSA signature. * Verify an ECDSA signature.
* @param {Buffer} key - A private or public key * @param {Buffer} publicKey - A 65-byte public key
* @param {Buffer} msg - The message being verified * @param {Buffer} msg - The message being verified
* @param {Buffer} sig - The signature * @param {Buffer} sig - The signature
* @return {Promise.<undefined>} A promise that resolves on correct * @return {Promise.<undefined>} A promise that resolves on correct
* signature and rejects on bad key or signature. * signature and rejects on bad key or signature.
*/ */
exports.verify = function(key, msg, sig) { exports.verify = function(publicKey, msg, sig) {
var publicKey = key.length === 32 ? getPublic(key) : key;
return new promise(function(resolve, reject) { return new promise(function(resolve, reject) {
return secp256k1.verify(publicKey, msg, sig) === 1 ? resolve() : reject(); return secp256k1.verify(publicKey, msg, sig) === 1 ? resolve() : reject();
}); });

View File

@ -38,14 +38,6 @@ 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) { it("shouldn't verify incorrect signature", 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;