eccrypto.generatePrivate() (#38)

* eccrypto.generatePrivate()

* Fix comment indentation
This commit is contained in:
Erik Nilsson 2019-03-24 02:02:32 +01:00 committed by Jordan Baczuk
parent 1ac5b09ce5
commit 98f7eec7e7
4 changed files with 46 additions and 10 deletions

View File

@ -43,7 +43,7 @@ var crypto = require("crypto");
var eccrypto = require("eccrypto");
// A new random 32-byte private key.
var privateKey = crypto.randomBytes(32);
var privateKey = eccrypto.generatePrivate();
// Corresponding uncompressed (65-byte) public key.
var publicKey = eccrypto.getPublic(privateKey);
@ -64,12 +64,11 @@ eccrypto.sign(privateKey, msg).then(function(sig) {
### ECDH
```js
var crypto = require("crypto");
var eccrypto = require("eccrypto");
var privateKeyA = crypto.randomBytes(32);
var privateKeyA = eccrypto.generatePrivate();
var publicKeyA = eccrypto.getPublic(privateKeyA);
var privateKeyB = crypto.randomBytes(32);
var privateKeyB = eccrypto.generatePrivate();
var publicKeyB = eccrypto.getPublic(privateKeyB);
eccrypto.derive(privateKeyA, publicKeyB).then(function(sharedKey1) {
@ -82,12 +81,11 @@ eccrypto.derive(privateKeyA, publicKeyB).then(function(sharedKey1) {
### ECIES
```js
var crypto = require("crypto");
var eccrypto = require("eccrypto");
var privateKeyA = crypto.randomBytes(32);
var privateKeyA = eccrypto.generatePrivate();
var publicKeyA = eccrypto.getPublic(privateKeyA);
var privateKeyB = crypto.randomBytes(32);
var privateKeyB = eccrypto.generatePrivate();
var publicKeyB = eccrypto.getPublic(privateKeyB);
// Encrypting the message for B.

View File

@ -112,6 +112,20 @@ function hmacSha256Verify(key, msg, sig) {
});
}
/**
* Generate a new valid private key. Will use the window.crypto or window.msCrypto as source
* depending on your browser.
* @return {Buffer} A 32-byte private key.
* @function
*/
exports.generatePrivate = function () {
var privateKey = randomBytes(32);
while (!isValidPrivateKey(privateKey)) {
privateKey = randomBytes(32);
}
return privateKey;
};
var getPublic = exports.getPublic = function(privateKey) {
// This function has sync API so we throw an error immediately.
assert(privateKey.length === 32, "Bad private key");

View File

@ -91,6 +91,19 @@ function pad32(msg){
}
}
/**
* Generate a new valid private key. Will use crypto.randomBytes as source.
* @return {Buffer} A 32-byte private key.
* @function
*/
exports.generatePrivate = function() {
var privateKey = crypto.randomBytes(32);
while (!isValidPrivateKey(privateKey)) {
privateKey = crypto.randomBytes(32);
}
return privateKey;
};
/**
* Compute the public key for a given private key.
* @param {Buffer} privateKey - A 32-byte private key

11
test.js
View File

@ -235,6 +235,17 @@ describe("ECIES", function() {
});
});
it("should encrypt and decrypt with generated private and public key", function () {
var privateKey = eccrypto.generatePrivate();
var publicKey = eccrypto.getPublic(privateKey);
return eccrypto.encrypt(publicKey, Buffer.from("generated private key"))
.then(function(enc) { return eccrypto.decrypt(privateKey, enc); })
.then(function(msg) {
expect(msg.toString()).to.equal("generated private key");
});
});
it("should reject promise on bad private key when decrypting", function(done) {
eccrypto.encrypt(publicKeyA, Buffer.from("test")).then(function(enc) {
eccrypto.decrypt(privateKeyB, enc).catch(function() {