Created
May 1, 2018 15:46
-
-
Save wesleyduff/a759431daf4fa2e8e6a5515e890ef0b6 to your computer and use it in GitHub Desktop.
Encrypt Decrypt data over HTTP calls | NODE.JS
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 crypto = require('crypto'); | |
const algorithm = 'aes-256-ctr'; | |
const password = '<your password>'; | |
class CryptoString { | |
static set(text){ | |
const cipher = crypto.createCipher(algorithm, password); | |
let crypted = cipher.update(text, 'utf8', 'hex'); | |
crypted += cipher.final('hex'); | |
return crypted; | |
} | |
static get(text) { | |
const decipher = crypto.createDecipher(algorithm, password); | |
let dec = decipher.update(text, 'hex', 'utf8'); | |
dec += decipher.final('utf8'); | |
return dec; | |
} | |
} | |
/** | |
making a fetch call | |
*/ | |
fetch(url, { | |
method: 'post' | |
headers : {...}, | |
body: JSON.stringify({data: CryptoString.set(JSON.stringify({ data .... }))}) | |
.... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment