Last active
December 5, 2022 11:39
-
-
Save devzarghami/4f018cf344f8ab98ebbfa3010770c572 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const algorithm = 'aes-256-ctr' | |
const secretKey = '654sd656s5ds5d4sd4s5d4sd' | |
function encrypt(string) { | |
const iv = crypto.randomBytes(16) | |
const cipher = crypto.createCipheriv(algorithm, secretKey, iv) | |
const encrypted = Buffer.concat([cipher.update(string), cipher.final()]) | |
return { | |
iv: iv.toString('hex'), | |
content: encrypted.toString('hex') | |
} | |
} | |
function decrypt(hash) { | |
const decipher = crypto.createDecipheriv(algorithm, secretKey, Buffer.from(hash.iv, 'hex')) | |
const decrypted = Buffer.concat([decipher.update(Buffer.from(hash.content, 'hex')), decipher.final()]) | |
return decrypted.toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment