Skip to content

Instantly share code, notes, and snippets.

@kkeeth
Created October 10, 2019 22:36
Show Gist options
  • Save kkeeth/f9070048330a46f054904bbbf7dd0a0a to your computer and use it in GitHub Desktop.
Save kkeeth/f9070048330a46f054904bbbf7dd0a0a to your computer and use it in GitHub Desktop.
encrypt decrypt sample js with moment
const crypto = require('crypto')
const moment = require('moment')
const algorithm = 'aes-256-cbc'
const password = "xrandom-password-length-32-chars"
const iv = 'random-length-16'
const hash = encrypt('hoge')
function encrypt(text){
const cipher = crypto.createCipheriv(algorithm,password,iv)
text += `\$${moment().unix()}`
let crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
const decipher = crypto.createDecipheriv(algorithm,password,iv)
let dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
console.log('**** encrypted ****')
console.log(encrypt(hash))
console.log('**** decrypted ****')
console.log(decrypt(hash))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment