class Siba::PasswordStrength

Password strength calculator based on:

http://snippets.dzone.com/posts/show/4698
https://www.grc.com/haystack.htm

Constants

AGE_OF_THE_UNIVERSE_SECONDS
AinB
Illions
PASSWORD_SETS
TRIES_PER_SECOND

Public Class Methods

is_weak?(seconds_to_crack) click to toggle source

Password is considered weak if it takes less than a year to crack it

# File lib/siba/helpers/password_strength.rb, line 46
def is_weak?(seconds_to_crack)
  seconds_to_crack < 60 * 60 * 24 * 365
end
seconds_to_crack(password) click to toggle source
# File lib/siba/helpers/password_strength.rb, line 37
def seconds_to_crack(password)
  set_size = 0
  PASSWORD_SETS.each_pair {|k,v| set_size += v if password =~ k}
  combinations = 0
  1.upto(password.length) {|i| combinations += set_size ** i }
  combinations.to_f / TRIES_PER_SECOND
end
seconds_to_timespan(seconds) click to toggle source

Convert the number of seconds human-friendly timespan string Example:

130: 2 minutes
12345: 3 hours
# File lib/siba/helpers/password_strength.rb, line 54
def seconds_to_timespan(seconds)
  return "forever" if seconds > AGE_OF_THE_UNIVERSE_SECONDS
  ticks = seconds
  AinB.each_pair do |a,b|
    ticks_next = ticks.to_f / b
    return get_timespan_str ticks, a if ticks_next < 1
    ticks = ticks_next
  end

  # century or longer
  ticks = ticks.floor
  return get_timespan_str ticks, "century", "centuries" if ticks < 100
  illion_unit, ticks = get_illions ticks
  "#{ticks} #{illion_unit} centuries".strip
end

Private Class Methods

get_illions(ticks) click to toggle source
# File lib/siba/helpers/password_strength.rb, line 81
def get_illions(ticks)
  illion_unit = ""
  Illions.each_pair do |a,b|
    ticks_next = ticks.to_f / b
    break if ticks_next < 1
    illion_unit = a
    ticks = ticks_next
  end

  return illion_unit, ticks.floor
end
get_timespan_str(ticks, unit, unit_plural=nil) click to toggle source
# File lib/siba/helpers/password_strength.rb, line 72
def get_timespan_str(ticks, unit, unit_plural=nil)
  ticks = ticks.floor
  return case
    when ticks < 1 then "less than a #{unit}"
    when ticks == 1 then "1 #{unit}"
    else "#{ticks} #{unit_plural || unit.to_s+"s"}"
  end
end