Module: Zxcvbn::Math
- Included in:
- Entropy
- Defined in:
- lib/zxcvbn/math.rb
Instance Method Summary collapse
- #average_degree_for_graph(graph_name) ⇒ Object
- #bruteforce_cardinality(password) ⇒ Object
- #lg(n) ⇒ Object
- #nCk(n, k) ⇒ Object
- #starting_positions_for_graph(graph_name) ⇒ Object
Instance Method Details
#average_degree_for_graph(graph_name) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/zxcvbn/math.rb', line 43 def average_degree_for_graph(graph_name) graph = data.adjacency_graphs[graph_name] degrees = graph.map { |_, neighbors| neighbors.compact.size } sum = degrees.inject(0, :+) sum.to_f / graph.size 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 |
#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
50 51 52 |
# File 'lib/zxcvbn/math.rb', line 50 def starting_positions_for_graph(graph_name) data.adjacency_graphs[graph_name].length end |