DIFF.BLOG
New
Following
Discover
Jobs
More
Suggest a blog
Upvotes plugin
Report bug
Contact
About
Sign up  
Encrypt and Decrypt in NodeJS
1
·
Alex Adam
·
Aug. 21, 2021, 10:27 a.m.
Summary
How to encrypt textCreate a file named encdec.js and paste:const crypto = require("crypto")const encrypt = (plainText, password) => { try { const iv = crypto.randomBytes(16); const key = crypto.createHash('sha256').update(password).digest('base64').substr(0, 32); const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); let encrypted = cipher.update(plainText); encrypted = Buffer.concat([encrypted, cipher.final()]) return iv.toString('hex') + ':' + encrypted.toString('hex'...
Read full post on alexadam.medium.com →
Submit
AUTHOR
RECENT POSTS FROM THE AUTHOR