Class: Zxcvbn::Data Private

Inherits:
Object
  • Object
show all
Defined in:
lib/zxcvbn/data.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Holds all loaded frequency lists, adjacency graphs, tries, and graph stats used by the matchers and scorer.

Constant Summary collapse

RESERVED_NAMES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Built-in dictionary names and the reserved per-call key. These names cannot be passed to TesterBuilder#add_word_list.

%w[
  english_wikipedia female_names male_names passwords surnames us_tv_and_film user_inputs
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeData

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads all built-in frequency lists and adjacency graphs from disk.



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

def initialize
  ranked = DictionaryRanker.rank_dictionaries(
    'english_wikipedia' => read_word_list('english_wikipedia.txt'),
    'female_names' => read_word_list('female_names.txt'),
    'male_names' => read_word_list('male_names.txt'),
    'passwords' => read_word_list('passwords.txt'),
    'surnames' => read_word_list('surnames.txt'),
    'us_tv_and_film' => read_word_list('us_tv_and_film.txt')
  ).tap { |r| r.each_value(&:freeze) }.freeze
  tries = build_tries(ranked).tap { |t| t.each_value(&:freeze) }.freeze
  @dictionaries = Dictionaries.new(ranked:, tries:)
  @adjacency_graphs =
    JSON.parse(DATA_PATH.join('adjacency_graphs.json').read)
        .tap { |gs| gs.each_value { |g| g.each_value { |a| a.each(&:freeze).freeze }.freeze } }
        .freeze
  @graph_stats = compute_graph_stats.each_value(&:freeze).freeze
end

Instance Attribute Details

#adjacency_graphsHash{String => Hash} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

keyboard adjacency data

Returns:

  • (Hash{String => Hash})

    the current value of adjacency_graphs



17
18
19
# File 'lib/zxcvbn/data.rb', line 17

def adjacency_graphs
  @adjacency_graphs
end

#dictionaries#ranked, #tries (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

consistent snapshot of ranked dictionaries and their tries; use this for concurrent reads to guarantee the pair is always in sync

Returns:

  • (#ranked, #tries)

    the current value of dictionaries



17
18
19
# File 'lib/zxcvbn/data.rb', line 17

def dictionaries
  @dictionaries
end

#graph_statsHash{String => Hash} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

precomputed average degree and key count

Returns:

  • (Hash{String => Hash})

    the current value of graph_stats



17
18
19
# File 'lib/zxcvbn/data.rb', line 17

def graph_stats
  @graph_stats
end

Instance Method Details

#add_word_list(name, list) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Adds a custom word list and builds a trie for it.

Parameters:

  • name (String)

    dictionary name (used as a key in #ranked_dictionaries)

  • list (Array<String>)

    ordered words (most common first)



67
68
69
70
71
72
73
74
# File 'lib/zxcvbn/data.rb', line 67

def add_word_list(name, list)
  ranked_dict = DictionaryRanker.rank_dictionary(list.select { |w| w.is_a?(String) }).freeze
  trie = Trie.from_ranked(ranked_dict).freeze
  @dictionaries = @dictionaries.with(
    ranked: @dictionaries.ranked.merge(name => ranked_dict).freeze,
    tries: @dictionaries.tries.merge(name => trie).freeze
  )
end

#dictionary_triesHash{String => Trie}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns prefix tries per dictionary.

Returns:

  • (Hash{String => Trie})

    prefix tries per dictionary



60
# File 'lib/zxcvbn/data.rb', line 60

def dictionary_tries = @dictionaries.tries

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
# File 'lib/zxcvbn/data.rb', line 54

def inspect = "#<#{self.class}:0x#{__id__.to_s(16)}>"

#ranked_dictionariesHash{String => Hash{String => Integer}}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns word → rank maps.

Returns:

  • (Hash{String => Hash{String => Integer}})

    word → rank maps



57
# File 'lib/zxcvbn/data.rb', line 57

def ranked_dictionaries = @dictionaries.ranked