Skip to content

Instantly share code, notes, and snippets.

@lapo-luchini
Last active May 7, 2022 12:50
Show Gist options
  • Save lapo-luchini/fb0cd5b40bd3c69e646868d3687bd884 to your computer and use it in GitHub Desktop.
Save lapo-luchini/fb0cd5b40bd3c69e646868d3687bd884 to your computer and use it in GitHub Desktop.
Automate solution of MD5LE
// https://md5le.vercel.app/
const
url = 'https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt',
fs = require('fs').promises,
hash = require('crypto').createHash('md5'),
rl = require('readline/promises').createInterface({
input: process.stdin,
output: process.stdout
}),
len = 32,
map = {};
async function load() {
let words;
try {
words = await fs.readFile('word5.txt', 'ascii');
words = words.trim().split(/\r?\n/);
} catch (e) {
words = await new Promise((accept, reject) => {
require('https').get(url, resp => {
if (resp.statusCode != 200)
reject(new Error('HTTP ' + resp.statusCode));
const cont = [];
resp.on('data', str => cont.push(str));
resp.on('end', () => accept(cont.join('')));
});
});
words = words.trim().split(/\r?\n/);
words = words.filter(w => w.length == 5);
await fs.writeFile('word5.txt', words.join('\n') + '\n', 'ascii');
}
for (let w of words) {
let h = hash.copy().update(w).digest().toString('hex');
map[h] = w;
}
}
async function solve() {
let h = Object.keys(map);
let possible = Array(len).fill('0123456789abcdef');
let q = process.argv.length > 2 ? process.argv[2] : h[0];
while (h.length > 1) {
console.log('Words: ' + h.length);
let a = await rl.question('Try: ' + q + '\nMatch> ');
for (let i = 0; i < len; ++i)
switch (a[i]) {
case '0': // black
case 'b':
if (i == q.indexOf(q[i])) // if first appearance of letter is black, it is not present
for (let j = 0; j < len; ++j)
possible[j] = possible[j].replace(q[i], '');
break;
case '2': // green
case 'g':
possible[i] = q[i];
break;
default: // yellow
possible[i] = possible[i].replace(q[i], '');
}
h = h.filter(w => {
for (let i = 0; i < len; ++i)
if (!possible[i].includes(w[i]))
return false;
return true;
});
q = h[0];
}
if (h.length == 0)
console.log('Solution not found!');
else
console.log('MD5: ' + h[0] + '\nWord: ' + map[h[0]]);
process.exit(0);
}
load().then(solve);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment