javascriptnode.jssjcl

Read a long file and use nodejs to crypt


This is my first question. English is not my first language. I have a file that contains around 800K lines. I have to read and encrypt them using the sjcl library. Until now, the only thing I did is this lines:

var sjcl = require('sjcl/sjcl.js');
var fs = require('fs'),
    readline = require('readline'),
    stream = require('stream');
var instream = fs.createReadStream('data');
var outstream = new stream;
outstream.readable = true;
outstream.writable = true;

var rl = readline.createInterface({
    input: instream,
    output: outstream,
    terminal: false
});

rl.on('line', function(line) {
    var cred = line.toLowerCase()+line;
    var salt = sjcl.codec.utf8String.toBits(cred);
    var id = sjcl.misc.scrypt(cred,salt,2048,8,1,32);
    var ret = sjcl.codec.hex.fromBits(id);
    console.log(ret+":"+line);
    });

It works, but this is very slow(10 lines/sec). Is there any way to speed up the process?


Solution

  • The scrypt algorithm you are using is intentionally slow and resource intensive to prevent brute force attacks against encrypted passwords (more info here: scrypt). Making it faster, as for example reducing the number of rounds, would weaken the strength of the encryption, so there is very little you can do.

    A possible solution to speed up your computation would be to use a native implementation of the algorithm, node-scrypt for example is a JS wrapper around the original C++ library, try it, you should definitely get a big improvement, since jscl is pure JavaScript.