Skip to content

Instantly share code, notes, and snippets.

@devzarghami
Last active December 5, 2022 11:39
Show Gist options
  • Save devzarghami/4f018cf344f8ab98ebbfa3010770c572 to your computer and use it in GitHub Desktop.
Save devzarghami/4f018cf344f8ab98ebbfa3010770c572 to your computer and use it in GitHub Desktop.
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