xxxxxxxxxx
69
/*
This program computes all possible alphanumeric passwords of
length three, and it uses that list to crack a fake password
that's stored in a separate file.
Only basic JS is used so that new programmers
can understand the code.
*/
// manually guess the password
// authenticate(password) returns true if correct, false if not
// note: the authenticator is defined in a separate file
console.log(authenticate('2fX'));
// now let's try to crack the password
// start by making an array of the possible characters
// and build all possible passwords (of length 3) from that
const CHARACTERS = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
];
// cartesianStrings(array1, array2)
// returns Cartesian product of two arrays of strings
// array1 = [x1, ... , xm] and array2 = [y1, ..., yn],
// with the ordered pair ('x','y') represented as 'xy' (for example)
/*
Note: with modern built-in JS functions, it's
possible to compute the Cartesian product of arbitrarily
many arrays in just one line of code. See
https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript
In the solution given there, n-tuples are represented as arrays
of length n, so it would need to be modified to return passwords
of length n as strings.
*/
function cartesianStrings(array1, array2) {
let product = [];
for (let i = 0; i < array1.length; i++) {
for (let j = 0; j < array2.length; j++) {
product.push(array1[i]+array2[j]);
}
}
return product;
}
const PASSWORDS = cartesianStrings(cartesianStrings(CHARACTERS, CHARACTERS), CHARACTERS);
// check that we have 62 ** 3 = 238328 passwords
console.log(PASSWORDS.length);
// use our list to crack the password!
for (let i = 0; i < PASSWORDS.length; i++) {
if (authenticate(PASSWORDS[i])) {
console.log(`Password: ${PASSWORDS[i]}`);
break;
}
}
// you can check authenticator.js to see if you got it correct :)