aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypt.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-08-12 19:00:50 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-08-12 19:00:50 +0200
commit74abb7052d3af3c305cfe3c525aea311ccc446e1 (patch)
tree752f6da94ea8b554906774d878ecebfbfe8d724e /lib/crypt.js
parent0660d3f5d3028ab3084c77f14a350d74c567bbd7 (diff)
downloadmy_apps-74abb7052d3af3c305cfe3c525aea311ccc446e1.tar.gz
my_apps-74abb7052d3af3c305cfe3c525aea311ccc446e1.tar.bz2
my_apps-74abb7052d3af3c305cfe3c525aea311ccc446e1.zip
crypt notes content
Diffstat (limited to 'lib/crypt.js')
-rw-r--r--lib/crypt.js19
1 files changed, 19 insertions, 0 deletions
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();
+}