Docs, cosmetics

This commit is contained in:
Kagami Hiiragi 2015-01-14 03:09:37 +03:00
parent bcc78bd8e4
commit 6aec8a3318
2 changed files with 13 additions and 9 deletions

View File

@ -39,16 +39,21 @@ So we use [seck256k1](https://www.npmjs.com/package/secp256k1) library in Node f
var crypto = require("crypto"); var crypto = require("crypto");
var eccrypto = require("eccrypto"); var eccrypto = require("eccrypto");
// A new random 32-byte private key.
var privateKey = crypto.randomBytes(32); var privateKey = crypto.randomBytes(32);
// Corresponding uncompressed (65-byte) public key.
var publicKey = eccrypto.getPublic(privateKey); var publicKey = eccrypto.getPublic(privateKey);
var str = "msg to sign";
var str = "message to sign";
// Always hash you message 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("Signature in DER format:", sig);
eccrypto.verify(publicKey, msg, sig).then(function() { eccrypto.verify(publicKey, msg, sig).then(function() {
console.log("verified"); console.log("Signature is OK");
}).catch(function() {
console.log("Signature is BAD");
}); });
}); });
``` ```

View File

@ -2,12 +2,6 @@
* Browser eccrypto implementation. * Browser eccrypto implementation.
*/ */
// NOTE(Kagami): We don't use promise shim in Browser implementation
// because it's supported natively in new browsers (see
// <http://caniuse.com/#feat=promises>) and we can use only new browsers
// because of the WebCryptoAPI (see
// <http://caniuse.com/#feat=cryptography>).
"use strict"; "use strict";
var EC = require("elliptic").ec; var EC = require("elliptic").ec;
@ -76,6 +70,11 @@ var getPublic = exports.getPublic = function(privateKey) {
return new Buffer(ec.keyPair(privateKey).getPublic("arr")); return new Buffer(ec.keyPair(privateKey).getPublic("arr"));
}; };
// NOTE(Kagami): We don't use promise shim in Browser implementation
// because it's supported natively in new browsers (see
// <http://caniuse.com/#feat=promises>) and we can use only new browsers
// because of the WebCryptoAPI (see
// <http://caniuse.com/#feat=cryptography>).
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");