Skip to content

Instantly share code, notes, and snippets.

@chrisinajar
Last active March 16, 2017 18:47
Show Gist options
  • Save chrisinajar/f212a34f3e4502d47c9f09b4d8d84405 to your computer and use it in GitHub Desktop.
Save chrisinajar/f212a34f3e4502d47c9f09b4d8d84405 to your computer and use it in GitHub Desktop.
Generate code that checks a password
function createChallenge (input, goal, hash) {
input = input.replace(/ /g, '');
var challengeLength = 16;
var challenge = hash(hash(input) + 'god damned internet').substr(0, challengeLength);
goal += challenge;
// after using input...
// right away just hash it, we never really want to work with the raw value
input = hash(input);
var outputLength = 0;
var fnOutput = "";
var currentIndentation = 0;
log('function checkPassphrase (input, hash) {');
currentIndentation++;
log('input = input.replace(/ /g, "");');
log('var challenge = hash(hash(input) + "god damned internet").substr(0, ' + challengeLength + ');');
log('input = hash(input);');
log('var output = "";');
while (outputLength < goal.length) {
var index = input.indexOf(goal[outputLength]);
if (index !== -1) {
log('output += input[' + index + '];');
outputLength++;
}
var salt = (Math.random() + '').substr(0, challengeLength);
input = hash(input + salt);
log('input = hash(input + "' + salt + '");');
}
log('if (output.substr(-' + challengeLength + ') === challenge) {');
currentIndentation++;
log('return output.substr(0, output.length - ' + challengeLength + ');');
currentIndentation--;
log('}');
log('return false;');
currentIndentation--;
log('}');
return fnOutput;
function log (msg) {
for (var i = 0; i < currentIndentation; ++i) {
msg = ' ' + msg;
}
console.log(msg);
fnOutput += msg + '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment