ecies-lite
Advanced tools
Comparing version 1.1.0 to 1.1.1
44
index.js
@@ -14,17 +14,22 @@ const crypto = require('crypto'); | ||
* Config the default parameters for ecies-lite | ||
* @param curveName: string - the elliptic curve to use | ||
* @param cipherAlgorithm: string - the cipher algorithm to use | ||
* @param hmacAlgorithm: string - the hmac algorithm to use | ||
* @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 curveName: string | object - the elliptic curve to use | the config object contains config items | ||
* @param cipherAlgorithm?: string - the cipher algorithm to use | ||
* @param hmacAlgorithm?: string - the hmac algorithm to use | ||
* @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 | ||
* @return none | ||
*/ | ||
exports.config = (curveName, cipherAlgorithm, hmacAlgorithm, ivSize, encKeyGen, macKeyGen) => { | ||
config.curveName = curveName || config.curveName; | ||
config.cipherAlgorithm = cipherAlgorithm || config.cipherAlgorithm; | ||
config.hmacAlgorithm = hmacAlgorithm || config.hmacAlgorithm; | ||
config.ivSize = ivSize || config.ivSize; | ||
config.encKeyGen = encKeyGen || config.encKeyGen; | ||
config.macKeyGen = macKeyGen || config.macKeyGen; | ||
if (typeof curveName === 'string') { | ||
config.curveName = curveName || config.curveName; | ||
config.cipherAlgorithm = cipherAlgorithm || config.cipherAlgorithm; | ||
config.hmacAlgorithm = hmacAlgorithm || config.hmacAlgorithm; | ||
config.ivSize = ivSize || config.ivSize; | ||
config.encKeyGen = encKeyGen || config.encKeyGen; | ||
config.macKeyGen = macKeyGen || config.macKeyGen; | ||
} | ||
else if (curveName instanceof Object) { | ||
Object.assign(config, curveName); | ||
} | ||
}; | ||
@@ -36,6 +41,4 @@ | ||
* @param msg: Buffer - The message to encrypt | ||
* @param ?opts: {?curveName: string, ?esk: Buffer, ?compressEpk: boolean, ?cipherAlgorithm: string, ?iv: Buffer}} opts - You can | ||
* specify the curve name, ephemeral private key, to compress ephemeral public key or not, cipher algorithm and initialization | ||
* vector to customize the output. | ||
* @return {epk: Buffer, iv: Buffer, ct: Buffer, mac: Buffer} - the ecies-lite structure with fields correspondingly stands for | ||
* @param opts?: the same structure as the config object - you can use it to specify advanced options | ||
* @return {epk: Buffer, iv: Buffer, ct: Buffer, mac: Buffer} - the ecies-lite structured object with fields correspondingly stands for | ||
* ephemeral public key, initialization vector, cipher text, mac code for above data, etc. | ||
@@ -71,6 +74,6 @@ */ | ||
* @param sk: Buffer - the recepient's private key | ||
* @param body: ecies-lite structure - the ecies-lite body (seen format in encrypt) to decrypt | ||
* @param ?opts: {?curveName: string, ?cipherAlgorithm: string} - to specify the curve name and cipher algorithm | ||
* @param body: ecies-lite structured object - the ecies-lite body (seen format in encrypt) to decrypt | ||
* @param opts?: the same structure as the config object - you can use it to specify advanced options | ||
* @return Buffer - the plain text decrypted from the Ecies-lite body | ||
* @throws when mac value is unmatched, throw a new Error('Corrupted Ecies-lite body: unmatched authentication code') | ||
* @throws when mac value is unmatched, it throws 'Corrupted Ecies-lite body: unmatched authentication code' error | ||
*/ | ||
@@ -90,4 +93,5 @@ exports.decrypt = (sk, body, opts) => { | ||
const mac = crypto.createHmac(opts.hmacAlgorithm, macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
if (mac.compare(body.mac) !== 0 || body.mac.compare(mac) !== 0) | ||
if (mac.compare(body.mac) !== 0 || body.mac.compare(mac) !== 0) { | ||
throw new Error('Corrupted Ecies-lite body: unmatched authentication code'); | ||
} | ||
const decipher = crypto.createDecipheriv(opts.cipherAlgorithm, encKey, iv); | ||
@@ -94,0 +98,0 @@ let pt = decipher.update(ct); |
{ | ||
"name": "ecies-lite", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A lightweight ECIES tool implemented in pure Node.JS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,2 +11,32 @@ # ecies-lite | ||
## API | ||
```config(curveName, cipherAlgorithm, hmacAlgorithm, ivSize, encKeyGen, macKeyGen)``` | ||
Config the default parameters for ecies-lite | ||
* @param curveName: string | object - the elliptic curve to use | the config object contains config items | ||
* @param cipherAlgorithm?: string - the cipher algorithm to use | ||
* @param hmacAlgorithm?: string - the hmac algorithm to use | ||
* @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 | ||
* @return none | ||
```encrypt(pk, msg, opts)``` | ||
Encrypt a message using the recepient's public key | ||
* @param pk: Buffer - The recipient's public key | ||
* @param msg: Buffer - The message to encrypt | ||
* @param opts?: the same structure as the config object - you can use it to specify advanced options | ||
* @return {epk: Buffer, iv: Buffer, ct: Buffer, mac: Buffer} - the ecies-lite structured object with fields correspondingly stands for | ||
* ephemeral public key, initialization vector, cipher text, mac code for above data, etc. | ||
```decrypt(sk, body, opts)``` | ||
Decrypt a message in ecies-lite defined format using the recipient's private key | ||
* @param sk: Buffer - the recepient's private key | ||
* @param body: ecies-lite structured object - the ecies-lite body (seen format in encrypt) to decrypt | ||
* @param opts?: the same structure as the config object - you can use it to specify advanced options | ||
* @return Buffer - the plain text decrypted from the Ecies-lite body | ||
* @throws when mac value is unmatched, it throws 'Corrupted Ecies-lite body: unmatched authentication code' error | ||
## Usage | ||
@@ -34,2 +64,18 @@ | ||
console.log('Decrypted plain text:', plain.toString('utf-8')); | ||
const curveName = 'prime256v1'; | ||
ecdh = crypto.createECDH(curveName); | ||
ecdh.generateKeys(); | ||
const ephemEcdh = crypto.createECDH(curveName); | ||
ephemEcdh.generateKeys(); | ||
const macKeyGen = (bytes) => { | ||
let buf = Buffer.from(bytes); | ||
for (let [index, value] of buf.entries()) { | ||
buf[index] = value ^ index; | ||
} | ||
return buf; | ||
} | ||
ecies.config({curveName, macKeyGen}); | ||
body = ecies.encrypt(recEcdh.getPublicKey(), Buffer.from('This message is to demo advanced usage'), {esk: ephemEcdh.getPrivateKey()}); | ||
console.log(ecies.decrypt(recEcdh.getPrivateKey(), body).toString('utf-8')); | ||
``` | ||
@@ -36,0 +82,0 @@ |
19
test.js
const crypto = require('crypto'), | ||
ecies = require('.'); | ||
ecies = require('.'); | ||
@@ -8,3 +8,3 @@ let recEcdh = crypto.createECDH(`secp256k1`); | ||
for (const k of Object.keys(body)) { | ||
console.log(`${k} (${body[k].length}B):`, body[k].toString('base64')); | ||
console.log(`${k} (${body[k].length}B):`, body[k].toString('base64')); | ||
} | ||
@@ -19,9 +19,10 @@ console.log(ecies.decrypt(recEcdh.getPrivateKey(), body).toString('utf-8')); | ||
const macKeyGen = (bytes) => { | ||
let buf = Buffer.from(bytes); | ||
for (let [index, value] of buf.entries()) { | ||
buf[index] = value | index; | ||
} | ||
return buf; | ||
let buf = Buffer.from(bytes); | ||
for (let [index, value] of buf.entries()) { | ||
buf[index] = value ^ index; | ||
} | ||
return buf; | ||
} | ||
body = ecies.encrypt(recEcdh.getPublicKey(), Buffer.from('This message is encrypted by ecies-lite with an assigned ephemeral key'), {esk: ephemEcdh.getPrivateKey(), curveName, macKeyGen}); | ||
console.log(ecies.decrypt(recEcdh.getPrivateKey(), body, {curveName, macKeyGen}).toString('utf-8')); | ||
ecies.config({curveName, macKeyGen}); | ||
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')); |
9689
113
87