class RubyEntropy

Constants

COMMON_PASSPHRASES

lowercase only

KEY_PATTERNS

Attributes

passphrase[R]

Public Class Methods

blacklist_passphrase(phrase) click to toggle source
# File lib/ruby-entropy.rb, line 17
def self.blacklist_passphrase(phrase)
  if phrase.class != String
    return 'you must enter a string'
  end
  COMMON_PASSPHRASES.push(phrase.downcase)
  return 'success'
end
new(passphrase) click to toggle source
# File lib/ruby-entropy.rb, line 9
def initialize(passphrase)
  @passphrase = passphrase
end

Public Instance Methods

strength() click to toggle source
# File lib/ruby-entropy.rb, line 13
def strength
  (31 * bad_passphrase_multiplier * Math.log(entropy / 13.62)).round(2)
end

Private Instance Methods

bad_passphrase_multiplier() click to toggle source
# File lib/ruby-entropy.rb, line 105
def bad_passphrase_multiplier
  repeaters || uniqueness ? (return 0.1) : 1
  key_pattern? || numerical_pattern? || repetitious? || common? ? @passphrase.length < 12 ? 0.5 : 0.75 : 1
end
common?() click to toggle source
# File lib/ruby-entropy.rb, line 77
def common?
  @passphrases = []
  @passphrases << @passphrase
  if @passphrase.match(/[@0|1$5]/)
    @passphrases << @passphrase.gsub('@', 'a').gsub('0', 'o').gsub(/[|1!]/, 'l').gsub(/[$5]/, 's')
    @passphrases << @passphrase.gsub('@', 'a').gsub('0', 'o').gsub(/[|1!]/, 'i').gsub(/[$5]/, 's')
  end
  COMMON_PASSPHRASES.each do |commoner|
    @passphrases.each { |passphrase| return true if passphrase.downcase.include?(commoner.downcase) }
  end
  false
end
count() click to toggle source
# File lib/ruby-entropy.rb, line 51
def count
  letters.to_i + multiple_cases.to_i + digits.to_i + symbols.to_i
end
digits() click to toggle source
# File lib/ruby-entropy.rb, line 43
def digits
  10 if @passphrase.match(/\d/) 
end
entropy() click to toggle source
# File lib/ruby-entropy.rb, line 27
def entropy
  Math.log2(count ** length)
end
key_pattern?() click to toggle source
# File lib/ruby-entropy.rb, line 55
def key_pattern?
  KEY_PATTERNS.each { |pattern| return true if @passphrase.downcase.include?(pattern) }
  false
end
length() click to toggle source
# File lib/ruby-entropy.rb, line 31
def length
  @passphrase.length
end
letters() click to toggle source
# File lib/ruby-entropy.rb, line 35
def letters
  26 if @passphrase.match(/[a-z]|[A-Z]/)
end
multiple_cases() click to toggle source
# File lib/ruby-entropy.rb, line 39
def multiple_cases
  26 if @passphrase.match(/[a-z]/) && @passphrase.match(/[A-Z]/)
end
numerical_pattern?() click to toggle source
# File lib/ruby-entropy.rb, line 60
def numerical_pattern?
  pattern = @passphrase.split('').map(&:to_i)
  pattern.each_with_index do |num, index|
    return true if pattern[index + 1] == num + 1 && pattern[index + 2] == num + 2 && pattern[index + 3] == num + 3
    return true if pattern[index + 1] == num - 1 && pattern[index + 2] == num - 2 && pattern[index + 3] == num - 3
  end
  false
end
repeaters() click to toggle source
# File lib/ruby-entropy.rb, line 94
def repeaters
  mode = []
  @passphrase.downcase.split('').uniq.each do |character|
    mode << @passphrase.split('').count(character)
  end
  mode.max.downto(2) do |num|
    return true if (mode.count(num)/mode.length.to_f) > 0.75
  end
  false
end
repetitious?() click to toggle source
# File lib/ruby-entropy.rb, line 69
def repetitious?
  characters = @passphrase.split('')
  characters.each_with_index do |character, index|
    return true if characters[index + 1] == character && characters[index + 2] == character
  end
  false
end
symbols() click to toggle source
# File lib/ruby-entropy.rb, line 47
def symbols
  33 if @passphrase.match(/\W/)
end
uniqueness() click to toggle source
# File lib/ruby-entropy.rb, line 90
def uniqueness
  (@passphrase.downcase.split('').uniq.length/length.to_f) < 0.4
end