Last active
June 21, 2019 14:42
-
-
Save Peleke/18198ab7130ff5564a83dd2d7e33308e to your computer and use it in GitHub Desktop.
tryAll
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
'use strict' | |
const fs = require('fs') | |
// Save this list as `eng_10k.txt`: https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english.txt | |
const _getFrequencyList = () => { | |
const frequencyList = fs.readFileSync(`${__dirname}/eng_10k.txt`).toString().split('\n').slice(1000) | |
const dict = {}; | |
frequencyList.forEach(word => { | |
// Ignore weird consonant clusters | |
// match returns `null` if `word` contains no vowels | |
if (!word.match(/[aeuoi]/gi)) { | |
return; | |
} | |
dict[word] = word; | |
}) | |
return dict; | |
} | |
const isEnglish = string => { | |
// Default threshold for how many English words we need before we consider the sentence "maybe English" | |
const threshold = 3; | |
if (string.split(/\s/).length < 6) { | |
// If this isn't a long enough sentence, be conservative: assume it's english | |
return true; | |
} else { | |
let count = 0; | |
const frequencyList = _getFrequencyList(); | |
string.split(/\s/).forEach(function (string) { | |
const adjusted = string.toLowerCase().replace(/\./g, '') | |
if (frequencyList[adjusted]) { | |
count += 1; | |
} | |
}) | |
return count > threshold; | |
} | |
} | |
module.exports = isEnglish; |
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
const caesar = require('./caesar'); | |
// Brute-force attack on Caesar's (non-)cipher | |
const tryAll = function (encryptedString) { | |
// The Caesar cipher | |
const decryptionAttempts = [] | |
while (decryptionAttempts.length < 24) { | |
const decryptionKey = -decryptionAttempts.length; | |
const decryptionAttempt = caesar(encryptedString, decryptionKey); | |
decryptionAttempts.push(decryptionAttempt) | |
} | |
return decryptionAttempts; | |
}; | |
const encryptedString = 'xt ny itjx.' | |
const decryptionAttempts = tryAll(encryptedString); | |
module.exports = tryAll; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment