From 74abb7052d3af3c305cfe3c525aea311ccc446e1 Mon Sep 17 00:00:00 2001 From: piotrruss Date: Thu, 12 Aug 2021 19:00:50 +0200 Subject: crypt notes content --- lib/crypt.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/crypt.js (limited to 'lib') diff --git a/lib/crypt.js b/lib/crypt.js new file mode 100644 index 0000000..4037c28 --- /dev/null +++ b/lib/crypt.js @@ -0,0 +1,19 @@ +const crypto = require('crypto') +const algorithm = 'aes-256-ctr' +const secretKey = 'QZmGk0WwHMIhDaIsgSqwdnrPjPM3VwN1' + +export const encrypt = (text) => { + const iv = crypto.randomBytes(16) + const cipher = crypto.createCipheriv(algorithm, secretKey, iv) + const encrypted = Buffer.concat([cipher.update(text), cipher.final()]) + + return iv.toString('hex') + '::' + encrypted.toString('hex') +} + +export const decrypt = (hash) => { + const [iv, content] = hash.split('::') + const decipher = crypto.createDecipheriv(algorithm, secretKey, Buffer.from(iv, 'hex')) + const decrpyted = Buffer.concat([decipher.update(Buffer.from(content, 'hex')), decipher.final()]) + + return decrpyted.toString(); +} -- cgit v1.2.3