eccrypto.generatePrivate() (#38)
* eccrypto.generatePrivate() * Fix comment indentation
This commit is contained in:
parent
1ac5b09ce5
commit
98f7eec7e7
12
README.md
12
README.md
|
@ -43,7 +43,7 @@ var crypto = require("crypto");
|
||||||
var eccrypto = require("eccrypto");
|
var eccrypto = require("eccrypto");
|
||||||
|
|
||||||
// A new random 32-byte private key.
|
// A new random 32-byte private key.
|
||||||
var privateKey = crypto.randomBytes(32);
|
var privateKey = eccrypto.generatePrivate();
|
||||||
// Corresponding uncompressed (65-byte) public key.
|
// Corresponding uncompressed (65-byte) public key.
|
||||||
var publicKey = eccrypto.getPublic(privateKey);
|
var publicKey = eccrypto.getPublic(privateKey);
|
||||||
|
|
||||||
|
@ -64,12 +64,11 @@ eccrypto.sign(privateKey, msg).then(function(sig) {
|
||||||
### ECDH
|
### ECDH
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var crypto = require("crypto");
|
|
||||||
var eccrypto = require("eccrypto");
|
var eccrypto = require("eccrypto");
|
||||||
|
|
||||||
var privateKeyA = crypto.randomBytes(32);
|
var privateKeyA = eccrypto.generatePrivate();
|
||||||
var publicKeyA = eccrypto.getPublic(privateKeyA);
|
var publicKeyA = eccrypto.getPublic(privateKeyA);
|
||||||
var privateKeyB = crypto.randomBytes(32);
|
var privateKeyB = eccrypto.generatePrivate();
|
||||||
var publicKeyB = eccrypto.getPublic(privateKeyB);
|
var publicKeyB = eccrypto.getPublic(privateKeyB);
|
||||||
|
|
||||||
eccrypto.derive(privateKeyA, publicKeyB).then(function(sharedKey1) {
|
eccrypto.derive(privateKeyA, publicKeyB).then(function(sharedKey1) {
|
||||||
|
@ -82,12 +81,11 @@ eccrypto.derive(privateKeyA, publicKeyB).then(function(sharedKey1) {
|
||||||
### ECIES
|
### ECIES
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var crypto = require("crypto");
|
|
||||||
var eccrypto = require("eccrypto");
|
var eccrypto = require("eccrypto");
|
||||||
|
|
||||||
var privateKeyA = crypto.randomBytes(32);
|
var privateKeyA = eccrypto.generatePrivate();
|
||||||
var publicKeyA = eccrypto.getPublic(privateKeyA);
|
var publicKeyA = eccrypto.getPublic(privateKeyA);
|
||||||
var privateKeyB = crypto.randomBytes(32);
|
var privateKeyB = eccrypto.generatePrivate();
|
||||||
var publicKeyB = eccrypto.getPublic(privateKeyB);
|
var publicKeyB = eccrypto.getPublic(privateKeyB);
|
||||||
|
|
||||||
// Encrypting the message for B.
|
// Encrypting the message for B.
|
||||||
|
|
14
browser.js
14
browser.js
|
@ -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) {
|
var getPublic = exports.getPublic = function(privateKey) {
|
||||||
// This function has sync API so we throw an error immediately.
|
// This function has sync API so we throw an error immediately.
|
||||||
assert(privateKey.length === 32, "Bad private key");
|
assert(privateKey.length === 32, "Bad private key");
|
||||||
|
|
13
index.js
13
index.js
|
@ -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.
|
* Compute the public key for a given private key.
|
||||||
* @param {Buffer} privateKey - A 32-byte private key
|
* @param {Buffer} privateKey - A 32-byte private key
|
||||||
|
|
11
test.js
11
test.js
|
@ -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) {
|
it("should reject promise on bad private key when decrypting", function(done) {
|
||||||
eccrypto.encrypt(publicKeyA, Buffer.from("test")).then(function(enc) {
|
eccrypto.encrypt(publicKeyA, Buffer.from("test")).then(function(enc) {
|
||||||
eccrypto.decrypt(privateKeyB, enc).catch(function() {
|
eccrypto.decrypt(privateKeyB, enc).catch(function() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user