Created
October 10, 2019 22:36
-
-
Save kkeeth/f9070048330a46f054904bbbf7dd0a0a to your computer and use it in GitHub Desktop.
encrypt decrypt sample js with moment
This file contains hidden or 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 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