Module: Zxcvbn::Math

Included in:
Entropy
Defined in:
lib/zxcvbn/math.rb

Instance Method Summary collapse

Instance Method Details

#average_degree_for_graph(graph_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zxcvbn/math.rb', line 47

def average_degree_for_graph(graph_name)
  graph   = Zxcvbn::ADJACENCY_GRAPHS[graph_name]
  average = 0.0

  graph.each do |key, neighbors|
    average += neighbors.compact.length
  end

  average /= graph.keys.length
  average
end

#bruteforce_cardinality(password) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zxcvbn/math.rb', line 3

def bruteforce_cardinality(password)
  is_type_of = {}

  password.each_byte do |ordinal|
    case ordinal
    when (48..57)
      is_type_of['digits'] = true
    when (65..90)
      is_type_of['upper'] = true
    when (97..122)
      is_type_of['lower'] = true
    else
      is_type_of['symbols'] = true
    end
  end

  cardinality = 0
  cardinality += 10 if is_type_of['digits']
  cardinality += 26 if is_type_of['upper']
  cardinality += 26 if is_type_of['lower']
  cardinality += 33 if is_type_of['symbols']
  cardinality
end

#lg(n) ⇒ Object



27
28
29
# File 'lib/zxcvbn/math.rb', line 27

def lg(n)
  ::Math.log(n, 2)
end

#min(a, b) ⇒ Object



43
44
45
# File 'lib/zxcvbn/math.rb', line 43

def min(a, b)
  a < b ? a : b
end

#nCk(n, k) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zxcvbn/math.rb', line 31

def nCk(n, k)
  return 0 if k > n
  return 1 if k == 0
  r = 1
  (1..k).each do |d|
    r = r * n
    r = r / d
    n -= 1
  end
  r
end

#starting_positions_for_graph(graph_name) ⇒ Object



59
60
61
# File 'lib/zxcvbn/math.rb', line 59

def starting_positions_for_graph(graph_name)
  Zxcvbn::ADJACENCY_GRAPHS[graph_name].length
end