ecies-lite
Advanced tools
Comparing version 1.1.2 to 1.1.3
58
index.js
@@ -41,28 +41,26 @@ const crypto = require('crypto'); | ||
* @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. | ||
* @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. | ||
*/ | ||
exports.encrypt = (pk, msg, opts) => { | ||
const t = Object.assign({}, config); | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
opts = Object.assign(t, opts); | ||
let ctx = Object.assign({}, config); | ||
ctx = Object.assign(ctx, opts || {}); | ||
const ecdh = crypto.createECDH(opts.curveName); | ||
if (opts.esk) { | ||
ecdh.setPrivateKey(opts.esk); | ||
const ecdh = crypto.createECDH(ctx.curveName); | ||
if (ctx.esk) { | ||
ecdh.setPrivateKey(ctx.esk); | ||
} else { | ||
ecdh.generateKeys(); | ||
} | ||
const epk = ecdh.getPublicKey(null, ctx.compressEpk ? 'compressed' : 'uncompressed'); | ||
const epk = ecdh.getPublicKey(null, opts.compressEpk ? 'compressed' : 'uncompressed'); | ||
const hash = crypto.createHash('sha256').update(ecdh.computeSecret(pk)).digest(); | ||
const cipherKey = opts.cipherKeyGen(hash), macKey = opts.hmacKeyGen(hash); | ||
const iv = opts.iv || crypto.randomBytes(config.ivSize); | ||
const cipher = crypto.createCipheriv(opts.cipherAlgorithm, cipherKey, iv); | ||
let ct = cipher.update(msg); | ||
ct = Buffer.concat([ct, cipher.final()]); | ||
const mac = crypto.createHmac(opts.hmacAlgorithm, macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
return {epk, iv, ct, mac}; | ||
with (ctx) { | ||
const hash = crypto.createHash('sha256').update(ecdh.computeSecret(pk)).digest(); | ||
const cipherKey = cipherKeyGen(hash), macKey = hmacKeyGen(hash); | ||
const iv = ctx.iv || crypto.randomBytes(ivSize); | ||
const cipher = crypto.createCipheriv(cipherAlgorithm, cipherKey, iv); | ||
let ct = cipher.update(msg); | ||
ct = Buffer.concat([ct, cipher.final()]); | ||
const mac = crypto.createHmac(hmacAlgorithm, macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
return {epk, iv, ct, mac}; | ||
} | ||
}; | ||
@@ -79,18 +77,16 @@ | ||
exports.decrypt = (sk, body, opts) => { | ||
const t = Object.assign({}, config); | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
opts = Object.assign(t, opts); | ||
let ctx = Object.assign({}, config); | ||
ctx = Object.assign(ctx, opts || {}); | ||
ctx = Object.assign(ctx, body); | ||
const ecdh = crypto.createECDH(opts.curveName); | ||
ecdh.setPrivateKey(sk); | ||
with (body) { | ||
with (ctx) { | ||
const ecdh = crypto.createECDH(curveName); | ||
ecdh.setPrivateKey(sk); | ||
const hash = crypto.createHash('sha256').update(ecdh.computeSecret(epk)).digest(); | ||
const cipherKey = opts.cipherKeyGen(hash), macKey = opts.hmacKeyGen(hash); | ||
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) { | ||
const cipherKey = cipherKeyGen(hash), macKey = hmacKeyGen(hash); | ||
const m = crypto.createHmac(hmacAlgorithm, macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
if (m.compare(mac) !== 0 || mac.compare(m) !== 0) { | ||
throw new Error('Corrupted Ecies-lite body: unmatched authentication code'); | ||
} | ||
const decipher = crypto.createDecipheriv(opts.cipherAlgorithm, cipherKey, iv); | ||
const decipher = crypto.createDecipheriv(cipherAlgorithm, cipherKey, iv); | ||
let pt = decipher.update(ct); | ||
@@ -97,0 +93,0 @@ return Buffer.concat([pt, decipher.final()]); |
{ | ||
"name": "ecies-lite", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "A lightweight ECIES tool implemented in pure Node.JS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,4 +29,3 @@ # ecies-lite | ||
* @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. | ||
* @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. | ||
@@ -33,0 +32,0 @@ ```decrypt(sk, body, opts)``` |
9909
111
86