UNPKG

ecies-lite

Version:

A lightweight ECIES tool implemented in pure Node.JS

29 lines (27 loc) 1.24 kB
const crypto = require('crypto'), ecies = require('.'); let recEcdh = crypto.createECDH(`secp256k1`); recEcdh.generateKeys(); let body = ecies.encrypt(recEcdh.getPublicKey(), Buffer.from('This message is encrypted by ecies-lite with default parameters')); 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')}'`); const curveName = 'prime256v1'; recEcdh = crypto.createECDH(curveName); recEcdh.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 encrypted by ecies-lite with customized options'), {esk: ephemEcdh.getPrivateKey()}); 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')}'`);