Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@authmane512
Created October 27, 2018 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save authmane512/9c74b0270a4b10a5ccc202136a6b000b to your computer and use it in GitHub Desktop.
Save authmane512/9c74b0270a4b10a5ccc202136a6b000b to your computer and use it in GitHub Desktop.
const nacl = require('tweetnacl');
const util = require('tweetnacl-util');
const scrypt = require('scryptsy');
const fs = require('fs');
let password = "Node.js is cool";
// these are our previous salt and nonce from encryption.js:
let salt = new Uint8Array([ 232, 66, 12, 24, 90, 175, 58, 106, 13, 220, 241, 77, 156, 230, 140, 218 ]);
let nonce = new Uint8Array([ 143, 28, 216, 227, 2, 177, 160, 159, 243, 180, 138, 230, 142, 165, 28, 189, 208, 63, 130, 131, 204, 240, 105, 142 ]);
// Generate the key:
let N = 16384; // number of iterations
let r = 8; // Memory factor
let p = 1; // Parallelization factor
let key = scrypt(password, salt, N, r, p, nacl.secretbox.keyLength);
// Get encrypted data from file:
let content = fs.readFileSync('file.txt', 'ascii'); // no need of utf8 with Base64
console.log(content);
let encrypted = util.decodeBase64(content);
// Decrypt the data:
let decrypted = nacl.secretbox.open(encrypted, nonce, key);
console.log(util.encodeUTF8(decrypted)); // function should be named "decodeUTF8", they reversed names!
@authmane512
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment