Skip to content

Instantly share code, notes, and snippets.

@wesleyduff
Created May 1, 2018 15:46
Show Gist options
  • Save wesleyduff/a759431daf4fa2e8e6a5515e890ef0b6 to your computer and use it in GitHub Desktop.
Save wesleyduff/a759431daf4fa2e8e6a5515e890ef0b6 to your computer and use it in GitHub Desktop.
Encrypt Decrypt data over HTTP calls | NODE.JS
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