made with esnextbin
Last active
June 1, 2017 20:55
-
-
Save n8io/e9dba14853bc30309bba01fccd22398c to your computer and use it in GitHub Desktop.
esnextbin sketch
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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
<pre id='code'></pre> | |
</body> | |
</html> |
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 randomstring = require('randomstring'); | |
const length = 32; | |
const ENCRYPTION_KEY = randomstring.generate({ length }); | |
const ENCRYPTION_ALGORITHM = 'aes-256-ctr'; | |
const IV_LENGTH = 16; // For AES, this is always 16 | |
const pii = { | |
firstName: 'Nate', | |
lastName: 'Clark', | |
ssn: '1234567890', | |
phones: { | |
mobile: '+12345678901', | |
}, | |
}; | |
function encrypt(text) { | |
let iv = crypto.randomBytes(IV_LENGTH); | |
let cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv); | |
let encrypted = cipher.update(text); | |
encrypted = Buffer.concat([encrypted, cipher.final()]); | |
return iv.toString('hex') + ':' + encrypted.toString('hex'); | |
} | |
function decrypt(text) { | |
let textParts = text.split(':'); | |
let iv = new Buffer(textParts.shift(), 'hex'); | |
let encryptedText = new Buffer(textParts.join(':'), 'hex'); | |
let decipher = crypto.createDecipheriv(ENCRYPTION_ALGORITHM, new Buffer(ENCRYPTION_KEY), iv); | |
let decrypted = decipher.update(encryptedText); | |
decrypted = Buffer.concat([decrypted, decipher.final()]); | |
return decrypted.toString(); | |
} | |
module.exports = { decrypt, encrypt }; |
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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"randomstring": "1.1.5", | |
"undefined": "v4.5.0" | |
} | |
} |
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
'use strict'; | |
var crypto = require('crypto'); | |
var randomstring = require('randomstring'); | |
var length = 32; | |
var ENCRYPTION_KEY = randomstring.generate({ length: length }); | |
var ENCRYPTION_ALGORITHM = 'aes-256-ctr'; | |
var IV_LENGTH = 16; // For AES, this is always 16 | |
var pii = { | |
firstName: 'Nate', | |
lastName: 'Clark', | |
ssn: '1234567890', | |
phones: { | |
mobile: '+12345678901' | |
} | |
}; | |
function encrypt(text) { | |
var iv = crypto.randomBytes(IV_LENGTH); | |
var cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv); | |
var encrypted = cipher.update(text); | |
encrypted = Buffer.concat([encrypted, cipher.final()]); | |
return iv.toString('hex') + ':' + encrypted.toString('hex'); | |
} | |
function decrypt(text) { | |
var textParts = text.split(':'); | |
var iv = new Buffer(textParts.shift(), 'hex'); | |
var encryptedText = new Buffer(textParts.join(':'), 'hex'); | |
var decipher = crypto.createDecipheriv(ENCRYPTION_ALGORITHM, new Buffer(ENCRYPTION_KEY), iv); | |
var decrypted = decipher.update(encryptedText); | |
decrypted = Buffer.concat([decrypted, decipher.final()]); | |
return decrypted.toString(); | |
} | |
module.exports = { decrypt: decrypt, encrypt: encrypt }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment