Created
May 10, 2012 15:55
-
-
Save chorn/2654087 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ruby | |
| require 'rubygems' | |
| require 'combinatorics' | |
| word_file = "/usr/share/dict/words" | |
| File.exists?(word_file) || puts("Missing #{word_file}") || exit | |
| original_target_length = 14 | |
| min_word_size = 3 | |
| max_word_size = 8 | |
| target_combination = nil | |
| until target_combination do | |
| target_words = rand(3) + 2 | |
| word_separators = [',', '.', ';', '-', '=', ''] | |
| word_separator = word_separators[rand(word_separators.size - 1)] | |
| target_length = original_target_length - (target_words * word_separator.length) | |
| combinatoric_expression = [] | |
| 1.upto(target_words) do | |
| combinatoric_expression << (min_word_size..max_word_size) | |
| end | |
| combinations_to_choose_from = [] | |
| combinatoric_expression.comprehension.to_a.each do |set| | |
| next unless set.size == target_words and set.inject(:+) == target_length | |
| combinations_to_choose_from << set | |
| end | |
| target_combination = combinations_to_choose_from[rand(combinations_to_choose_from.size - 1)] | |
| end | |
| # Wordlist | |
| words = {} | |
| min_word_size.upto(max_word_size) do |i| | |
| words[i] = [] | |
| end | |
| IO.readlines(word_file).each do |word| | |
| word.chomp! | |
| words[word.length] << word unless word =~ /[A-Z]/ or word.length < min_word_size or word.length > max_word_size | |
| end | |
| # Choose | |
| password = [] | |
| target_combination.each do |word_length| | |
| count = words[word_length].size | |
| password << words[word_length][rand(count - 1)] | |
| end | |
| puts password.join(word_separator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment