Skip to content

Instantly share code, notes, and snippets.

@krnlde
Last active July 15, 2017 17:06
Show Gist options
  • Save krnlde/f40123149c435ec4cecdd47e5b6b5f15 to your computer and use it in GitHub Desktop.
Save krnlde/f40123149c435ec4cecdd47e5b6b5f15 to your computer and use it in GitHub Desktop.
Clustered bruteforce password-cracking example
const cluster = require('cluster');
const numCPUs = require('os').cpus().length - 1;
const {promisify} = require('util');
const pbkdf2 = promisify(require('crypto').pbkdf2);
if (cluster.isMaster) {
const workers = [];
console.log(`Starting ${numCPUs} workers…`)
for (let i = 0; i < numCPUs; i += 1) {
workers.push(cluster.fork());
}
(async function bruteforce() {
const theSecret = (await pbkdf2('500', 'salt', 1e5, 32, 'sha512')).toString('hex');
let sample = 0;
console.time('bruteforce');
workers.forEach(worker => {
worker.send(sample++);
worker.on('message', ({secret, hash}) => {
if (theSecret != hash) return worker.send(sample++);
console.timeEnd('bruteforce');
console.log(`Your secret is ${secret}`);
workers.forEach(worker => worker.kill());
});
});
})();
return;
}
// Worker Code
process.on('message', (sample) => {
(async () => {
console.log(`computing "${sample}"…`);
const hash = (await pbkdf2(String(sample), 'salt', 1e5, 32, 'sha512')).toString('hex');
if (process.connected) process.send({secret: sample, hash});
})();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment