Class: Siba::PasswordStrength

Inherits:
Object
  • Object
show all
Defined in:
lib/siba/helpers/password_strength.rb

Overview

Password strength calculator based on:

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

Constant Summary collapse

PASSWORD_SETS =
{
  /[a-z]/ => 26,
  /[A-Z]/ => 26,
  /[0-9]/ => 10,
  /[^\w]/ => 33
}
AinB =
{
  second: 60,
  minute: 60,
  hour: 24,
  day: 30,
  month: 12,
  year: 100,
  century: 1
}
Illions =
{
  hundred: 100,
  thousand: 10,
  million: 1000,
  billion: 1000,
  trillion: 1000
}
TRIES_PER_SECOND =

100 TFLOPS

100 * 10 ** 12
AGE_OF_THE_UNIVERSE_SECONDS =

the best estimate in 2011

4.336 * 10 ** 17

Class Method Summary collapse

Class Method Details

.is_weak?(seconds_to_crack) ⇒ Boolean

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

Returns:

  • (Boolean)


46
47
48
# 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) ⇒ Object



37
38
39
40
41
42
43
# 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) ⇒ Object

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

130: 2 minutes
12345: 3 hours


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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