Last active
March 4, 2016 00:26
-
-
Save DanielFGray/93b4dcbfc99b442a80dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
var prompt = require('prompt'); | |
var argv = require('minimist')(process.argv.slice(2), { | |
alias: { | |
h: 'help', | |
v: 'version', | |
s: 'string', | |
} | |
}); | |
if(argv.help) { | |
console.log('Usage: snatch [options] [tags]\n'); | |
console.log('Options:'); | |
console.log(' '); | |
console.log(' -s, --string '); | |
console.log(' -h, --help print this help'); | |
console.log(' -v, --version print version number'); | |
process.exit(0); | |
} | |
if(argv.version) { | |
console.log('0.1'); | |
process.exit(0); | |
} | |
var globals = {}; | |
globals.main = function(userprompt) { | |
prompt.message = ''; | |
prompt.delimiter = ''; | |
prompt.override = argv; | |
prompt.start(); | |
prompt.get([{ | |
name: 'string', | |
type: 'string', | |
description: 'Enter the encrypted string:', | |
}], function(err, userprompt) { | |
globals.cryptedstring = userprompt.string.toLowerCase().split(''); | |
globals.promptreplace(); | |
}); | |
}; | |
globals.promptreplace = function() { | |
globals.freqcount = Cipher.getCharFreq(globals.cryptedstring); | |
if(globals.freqcount === false) { | |
console.log('done!'); | |
return true; | |
} | |
console.log(globals.freqcount); | |
prompt.start(); | |
prompt.get([{ | |
name: 'replace', | |
type: 'string', | |
pattern: /[a-zA-Z]=[a-zA-Z]?$/, | |
description: 'Try a replacement:', | |
message: 'must be x=y format' | |
}], function(err, userprompt) { | |
var sp = userprompt.replace.split('='); | |
Cipher.addMap(sp[0], sp[1]); | |
console.log(Cipher.rotate(globals.cryptedstring, Cipher.keymap)); | |
globals.promptreplace(); | |
}); | |
}; | |
var Cipher = { | |
keymap: {} | |
}; | |
Cipher.addMap = function(x, y) { | |
if(! y) { | |
delete Cipher.keymap[x]; | |
} | |
else { | |
for(var prop in Cipher.keymap) { | |
if(Cipher.keymap.hasOwnProperty(prop) && Cipher.keymap[prop] === y ) { | |
delete Cipher.keymap[prop]; | |
break; | |
} | |
} | |
Cipher.keymap[x] = y; | |
} | |
console.log(Cipher.keymap); | |
}; | |
Cipher.rotate = function(str, map) { | |
return str.map(function(e) { | |
if(map.hasOwnProperty(e)) { | |
return map[e.toLowerCase()].toUpperCase(); | |
} | |
else return e; | |
}) | |
.join(''); | |
}; | |
Cipher.getCharFreq = function(input) { | |
var freq = {}; | |
if(typeof input === 'string') { | |
input = input.split(''); | |
} | |
if(! Array.isArray(input)) { | |
return false; | |
} | |
input.forEach(function(e) { | |
if(e.match(/^\w$/)){ | |
if(freq[e] === undefined) { | |
freq[e] = 0; | |
} | |
freq[e]++; | |
} | |
}); | |
var str = Object.keys(freq).filter(function(e) { | |
return Cipher.keymap[e] ? false : true; | |
}); | |
if(str.length === 0) { | |
return true; | |
} | |
return str.sort(function(a, b) { | |
return freq[a] > freq[b] ? -1 : freq[a] < freq[b] ? 1 : 0; | |
}) | |
.map(function(k) { | |
return String(k).toLowerCase() + ':' + String(freq[k]); | |
}) | |
.join(', '); | |
}; | |
globals.main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment