ecies-lite
Advanced tools
Comparing version 1.0.6 to 1.0.7
53
index.js
@@ -33,20 +33,23 @@ const crypto = require('crypto'); | ||
exports.encrypt = (pk, msg, opts) => { | ||
if (!opts) | ||
opts = { | ||
curveName: config.curveName, | ||
compressEpk: true, | ||
cipherAlgorithm: config.cipherAlgorithm | ||
}; | ||
const t = Object.assign({}, config); | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
opts = Object.assign(t, opts); | ||
const ecdh = crypto.createECDH(opts.curveName || config.curveName); | ||
if (opts.esk) ecdh.setPrivateKey(opts.esk); | ||
else ecdh.generateKeys(); | ||
let epk = ecdh.getPublicKey(null, opts.compressEpk ? 'compressed' : 'uncompressed'); | ||
let hash = crypto.createHash('sha256').update(ecdh.computeSecret(pk)).digest(); | ||
let encKey = hash.slice(0, 32), macKey = hash.slice(16); | ||
let iv = opts.iv || crypto.randomBytes(config.ivSize); | ||
let cipher = crypto.createCipheriv(opts.cipherAlgorithm || config.cipherAlgorithm, encKey, iv); | ||
const ecdh = crypto.createECDH(opts.curveName); | ||
if (opts.esk) { | ||
ecdh.setPrivateKey(opts.esk); | ||
} else { | ||
ecdh.generateKeys(); | ||
} | ||
const epk = ecdh.getPublicKey(null, opts.compressEpk ? 'compressed' : 'uncompressed'); | ||
const hash = crypto.createHash('sha256').update(ecdh.computeSecret(pk)).digest(); | ||
const encKey = hash.slice(0, 32), macKey = hash.slice(16); | ||
const iv = opts.iv || crypto.randomBytes(config.ivSize); | ||
const cipher = crypto.createCipheriv(opts.cipherAlgorithm, encKey, iv); | ||
let ct = cipher.update(msg); | ||
ct = Buffer.concat([ct, cipher.final()]); | ||
let mac = crypto.createHmac('sha256', macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
const mac = crypto.createHmac('sha256', macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
return {epk, iv, ct, mac}; | ||
@@ -63,17 +66,17 @@ }; | ||
exports.decrypt = (sk, body, opts) => { | ||
if (!opts) | ||
opts = { | ||
curveName: config.curveName, | ||
cipherAlgorithm: config.cipherAlgorithm | ||
}; | ||
const t = Object.assign({}, config); | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
opts = Object.assign(t, opts); | ||
const ecdh = crypto.createECDH(opts.curveName || config.curveName); | ||
const ecdh = crypto.createECDH(opts.curveName); | ||
ecdh.setPrivateKey(sk); | ||
with (body) { | ||
let hash = crypto.createHash('sha256').update(ecdh.computeSecret(epk)).digest(); | ||
let encKey = hash.slice(0, 32), macKey = hash.slice(16); | ||
let mac = crypto.createHmac('sha256', macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
const hash = crypto.createHash('sha256').update(ecdh.computeSecret(epk)).digest(); | ||
const encKey = hash.slice(0, 32), macKey = hash.slice(16); | ||
const mac = crypto.createHmac('sha256', macKey).update(Buffer.concat([epk, iv, ct])).digest(); | ||
if (mac.compare(body.mac) !== 0 || body.mac.compare(mac) !== 0) | ||
throw new Error('Corrupted Ecies-lite body: unmatched authentication code'); | ||
let decipher = crypto.createDecipheriv(opts.cipherAlgorithm || config.cipherAlgorithm, encKey, iv); | ||
const decipher = crypto.createDecipheriv(opts.cipherAlgorithm, encKey, iv); | ||
let pt = decipher.update(ct); | ||
@@ -80,0 +83,0 @@ return Buffer.concat([pt, decipher.final()]); |
{ | ||
"name": "ecies-lite", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "A lightweight ECIES tool implemented in pure Node.JS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
const crypto = require('crypto'), | ||
ecies = require('./index.js'); | ||
ecies = require('.'); | ||
@@ -7,3 +7,3 @@ let recEcdh = crypto.createECDH(`secp256k1`); | ||
let body = ecies.encrypt(recEcdh.getPublicKey(), Buffer.from('This message is encrypted by ecies-lite with default parameters')); | ||
for (let k of Object.keys(body)) { | ||
for (const k of Object.keys(body)) { | ||
console.log(`${k} (${body[k].length}B):`, body[k].toString('base64')); | ||
@@ -16,5 +16,5 @@ } | ||
recEcdh.generateKeys(); | ||
let ephemEcdh = crypto.createECDH(curveName); | ||
const ephemEcdh = crypto.createECDH(curveName); | ||
ephemEcdh.generateKeys(); | ||
body = ecies.encrypt(recEcdh.getPublicKey(), Buffer.from('This message is encrypted by ecies-lite with an assigned ephemeral key'), {esk: ephemEcdh.getPrivateKey(), curveName}); | ||
console.log(ecies.decrypt(recEcdh.getPrivateKey(), body, {curveName}).toString('utf-8')); | ||
console.log(ecies.decrypt(recEcdh.getPrivateKey(), body, {curveName}).toString('utf-8')); |
92
6575