Sha256: ddd060fa36f2440b0369980f516004e73d81c29eb191e450662abdfc87e566e2

Contents?: true

Size: 930 Bytes

Versions: 6

Compression:

Stored size: 930 Bytes

Contents

require 'securerandom'

#
# Generate an N-word passphrase from a corpus
#
class CorrectHorseBatteryStaple::Generator
  include CorrectHorseBatteryStaple::Common
  include CorrectHorseBatteryStaple::Memoize

  attr_accessor :word_length, :corpus

  def initialize(corpus, word_length = nil)
    @corpus      = corpus
    if word_length
      @corpus.filter {|entry| word_length.include?(entry.word.to_s.length) }
    end
  end

  def make(count = 4, options = {})
    @corpus.pick(count, options).
      map {|entry| entry.word.downcase }.
      join("-")
  end

  def estimate_entropy(options)
    candidate_count = @corpus.count_candidates(options)
    (log(candidate_count) / log(2)).floor
  end
  memoize :estimate_entropy

  def words
    @words ||= @corpus.result
  end
end

if __FILE__ == $0
  puts CorrectHorseBatteryStaple::Generator.new(CorrectHorseBatteryStaple.default_corpus, 3..6).
    make((ARGV[0] || 4).to_i)
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
correct-horse-battery-staple-0.6.6 lib/correct_horse_battery_staple/generator.rb
correct-horse-battery-staple-0.6.5 lib/correct_horse_battery_staple/generator.rb
correct-horse-battery-staple-0.6.4 lib/correct_horse_battery_staple/generator.rb
correct-horse-battery-staple-0.6.3 lib/correct_horse_battery_staple/generator.rb
correct-horse-battery-staple-0.6.2 lib/correct_horse_battery_staple/generator.rb
correct-horse-battery-staple-0.6.1 lib/correct_horse_battery_staple/generator.rb