ecies-lite
Advanced tools
Comparing version 1.1.1 to 1.1.2
24
index.js
@@ -8,4 +8,4 @@ const crypto = require('crypto'); | ||
ivSize: 16, | ||
encKeyGen: (bytes) => bytes.slice(0, 32), | ||
macKeyGen: (bytes) => bytes.slice(16) | ||
cipherKeyGen: (bytes) => bytes.slice(0, 32), | ||
hmacKeyGen: (bytes) => bytes.slice(16) | ||
}; | ||
@@ -19,7 +19,7 @@ | ||
* @param ivSize?: number - the size (in bytes) of initialization vector (for cipher) | ||
* @param encKeyGen?: (Buffer) -> Buffer - the encrypt key generator | ||
* @param macKeyGen?: (Buffer) -> Buffer - the mac key generator | ||
* @param cipherKeyGen?: (Buffer) -> Buffer - the cipher key generator | ||
* @param hmacKeyGen?: (Buffer) -> Buffer - the hmac key generator | ||
* @return none | ||
*/ | ||
exports.config = (curveName, cipherAlgorithm, hmacAlgorithm, ivSize, encKeyGen, macKeyGen) => { | ||
exports.config = (curveName, cipherAlgorithm, hmacAlgorithm, ivSize, cipherKeyGen, hmacKeyGen) => { | ||
if (typeof curveName === 'string') { | ||
@@ -30,4 +30,4 @@ config.curveName = curveName || config.curveName; | ||
config.ivSize = ivSize || config.ivSize; | ||
config.encKeyGen = encKeyGen || config.encKeyGen; | ||
config.macKeyGen = macKeyGen || config.macKeyGen; | ||
config.cipherKeyGen = cipherKeyGen || config.cipherKeyGen; | ||
config.hmacKeyGen = hmacKeyGen || config.hmacKeyGen; | ||
} | ||
@@ -63,5 +63,5 @@ else if (curveName instanceof Object) { | ||
const hash = crypto.createHash('sha256').update(ecdh.computeSecret(pk)).digest(); | ||
const encKey = opts.encKeyGen(hash), macKey = opts.macKeyGen(hash); | ||
const cipherKey = opts.cipherKeyGen(hash), macKey = opts.hmacKeyGen(hash); | ||
const iv = opts.iv || crypto.randomBytes(config.ivSize); | ||
const cipher = crypto.createCipheriv(opts.cipherAlgorithm, encKey, iv); | ||
const cipher = crypto.createCipheriv(opts.cipherAlgorithm, cipherKey, iv); | ||
let ct = cipher.update(msg); | ||
@@ -92,3 +92,3 @@ ct = Buffer.concat([ct, cipher.final()]); | ||
const hash = crypto.createHash('sha256').update(ecdh.computeSecret(epk)).digest(); | ||
const encKey = opts.encKeyGen(hash), macKey = opts.macKeyGen(hash); | ||
const cipherKey = opts.cipherKeyGen(hash), macKey = opts.hmacKeyGen(hash); | ||
const mac = crypto.createHmac(opts.hmacAlgorithm, macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
@@ -98,6 +98,6 @@ if (mac.compare(body.mac) !== 0 || body.mac.compare(mac) !== 0) { | ||
} | ||
const decipher = crypto.createDecipheriv(opts.cipherAlgorithm, encKey, iv); | ||
const decipher = crypto.createDecipheriv(opts.cipherAlgorithm, cipherKey, iv); | ||
let pt = decipher.update(ct); | ||
return Buffer.concat([pt, decipher.final()]); | ||
} | ||
}; | ||
}; |
{ | ||
"name": "ecies-lite", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "A lightweight ECIES tool implemented in pure Node.JS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,6 +9,6 @@ # ecies-lite | ||
* Multiple Curves, KDF: SHA-256, HMAC: MAC-WITH-SHA-256, Multiple Cipher Algorithms | ||
* Multiple Curves, KDF: SHA-256 (default & customizable), HMAC: MAC-WITH-SHA-256 (default & customizable), Multiple Cipher Algorithms | ||
## API | ||
```config(curveName, cipherAlgorithm, hmacAlgorithm, ivSize, encKeyGen, macKeyGen)``` | ||
```config(curveName, cipherAlgorithm, hmacAlgorithm, ivSize, cipherKeyGen, hmacKeyGen)``` | ||
@@ -20,4 +20,4 @@ Config the default parameters for ecies-lite | ||
* @param ivSize?: number - the size (in bytes) of initialization vector (for cipher) | ||
* @param encKeyGen?: (Buffer) -> Buffer - the encrypt key generator | ||
* @param macKeyGen?: (Buffer) -> Buffer - the mac key generator | ||
* @param cipherKeyGen?: (Buffer) -> Buffer - the cipher key generator | ||
* @param hmacKeyGen?: (Buffer) -> Buffer - the hmac key generator | ||
* @return none | ||
@@ -24,0 +24,0 @@ |
@@ -10,3 +10,3 @@ const crypto = require('crypto'), | ||
} | ||
console.log(ecies.decrypt(recEcdh.getPrivateKey(), body).toString('utf-8')); | ||
console.log(`Decrypted plaintext: '${ecies.decrypt(recEcdh.getPrivateKey(), body).toString('utf-8')}'`); | ||
@@ -27,2 +27,5 @@ const curveName = 'prime256v1'; | ||
body = ecies.encrypt(recEcdh.getPublicKey(), Buffer.from('This message is encrypted by ecies-lite with customized options'), {esk: ephemEcdh.getPrivateKey()}); | ||
console.log(ecies.decrypt(recEcdh.getPrivateKey(), body).toString('utf-8')); | ||
for (const k of Object.keys(body)) { | ||
console.log(`${k} (${body[k].length}B):`, body[k].toString('base64')); | ||
} | ||
console.log(`Decrypted plaintext: '${ecies.decrypt(recEcdh.getPrivateKey(), body).toString('utf-8')}'`); |
9960
115