aboutsummaryrefslogtreecommitdiffstats
path: root/helpers/crypt.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-09-02 22:28:11 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-09-02 23:54:56 +0200
commitf08f6ca0a9d337efff280d4d1669a41b5d9c31c2 (patch)
tree7dee778ba742deb5f499f2aa08a1ba040606d633 /helpers/crypt.js
parent9f74c550927671f4ded301d0cf3e9d592716375c (diff)
downloadmy_apps-f08f6ca0a9d337efff280d4d1669a41b5d9c31c2.tar.gz
my_apps-f08f6ca0a9d337efff280d4d1669a41b5d9c31c2.tar.bz2
my_apps-f08f6ca0a9d337efff280d4d1669a41b5d9c31c2.zip
finish translations, force maximize
Diffstat (limited to 'helpers/crypt.js')
-rw-r--r--helpers/crypt.js19
1 files changed, 19 insertions, 0 deletions
diff --git a/helpers/crypt.js b/helpers/crypt.js
new file mode 100644
index 0000000..5d3d79e
--- /dev/null
+++ b/helpers/crypt.js
@@ -0,0 +1,19 @@
+const crypto = require('crypto')
+const algorithm = 'aes-256-ctr'
+const secretKey = process.env.MYAPPS_NOTES_KEY
+
+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();
+}