Fix API
This commit is contained in:
parent
9a9555a797
commit
4e3f857332
16
README.md
16
README.md
|
@ -38,6 +38,8 @@ So we use [seck256k1](https://www.npmjs.com/package/secp256k1) library in Node f
|
|||
|
||||
## Usage
|
||||
|
||||
### ECDSA
|
||||
|
||||
```js
|
||||
var crypto = require("crypto");
|
||||
var eccrypto = require("eccrypto");
|
||||
|
@ -45,19 +47,27 @@ var eccrypto = require("eccrypto");
|
|||
var privateKey = crypto.randomBytes(32);
|
||||
var publicKey = eccrypto.getPublic(privateKey);
|
||||
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();
|
||||
|
||||
eccrypto.sign(privateKey, msg).then(function(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() {
|
||||
console.log("verified");
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### ECDH
|
||||
|
||||
```js
|
||||
```
|
||||
|
||||
### ECIES
|
||||
|
||||
```js
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
eccrypto - JavaScript Elliptic curve cryptography library
|
||||
|
|
5
index.js
5
index.js
|
@ -33,14 +33,13 @@ exports.sign = function(privateKey, msg) {
|
|||
|
||||
/**
|
||||
* 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} sig - The signature
|
||||
* @return {Promise.<undefined>} A promise that resolves on correct
|
||||
* signature and rejects on bad key or signature.
|
||||
*/
|
||||
exports.verify = function(key, msg, sig) {
|
||||
var publicKey = key.length === 32 ? getPublic(key) : key;
|
||||
exports.verify = function(publicKey, msg, sig) {
|
||||
return new promise(function(resolve, reject) {
|
||||
return secp256k1.verify(publicKey, msg, sig) === 1 ? resolve() : reject();
|
||||
});
|
||||
|
|
8
test.js
8
test.js
|
@ -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) {
|
||||
eccrypto.sign(privateKey, msg).then(function(sig) {
|
||||
expect(Buffer.isBuffer(sig)).to.be.true;
|
||||
|
|
Loading…
Reference in New Issue
Block a user