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



50
51
52
# File 'lib/zxcvbn/math.rb', line 50

def average_degree_for_graph(graph_name)
  data.graph_stats[graph_name][:average_degree]
end

#bruteforce_cardinality(password) ⇒ Object



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

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



29
30
31
# File 'lib/zxcvbn/math.rb', line 29

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

#nCk(n, k) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zxcvbn/math.rb', line 33

def nCk(n, k)
  return 0 if k > n
  return 1 if k.zero?

  # Use symmetry property: C(n,k) = C(n, n-k)
  # Choose smaller k to minimize iterations
  k = n - k if k > n - k

  r = 1
  (1..k).each do |d|
    r *= n
    r /= d
    n -= 1
  end
  r
end

#starting_positions_for_graph(graph_name) ⇒ Object



54
55
56
# File 'lib/zxcvbn/math.rb', line 54

def starting_positions_for_graph(graph_name)
  data.graph_stats[graph_name][:starting_positions]
end