Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Commit

Permalink
Change the API to Zxcvbn.test('password') rather than including Zxcvb…
Browse files Browse the repository at this point in the history
…n to get the zxcvbn method.
  • Loading branch information
stevehodgkiss committed Oct 29, 2012
1 parent a97e1ea commit 744ba9d
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 49 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ Example usage:
require 'zxcvbn'

class User < Struct.new(:username, :password)
include Zxcvbn

def password_strength
zxcvbn(password, [username, 'sitename'])
Zxcvbn.test(password, [username, 'sitename'])
end
end

Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ require "bundler/setup"

task :console do
require 'zxcvbn'
include Zxcvbn
require './spec/support/js_helpers'
include JsHelpers
require 'irb'
Expand Down
17 changes: 5 additions & 12 deletions lib/zxcvbn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'zxcvbn/matchers/digits'
require 'zxcvbn/matchers/year'
require 'zxcvbn/matchers/date'
require 'zxcvbn/dictionary_ranker'
require 'zxcvbn/omnimatch'
require 'zxcvbn/math'
require 'zxcvbn/entropy'
Expand All @@ -18,18 +19,10 @@
require 'zxcvbn/password_strength'

module Zxcvbn
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def zxcvbn(password, user_inputs = [])
@zxcvbn ||= PasswordStrength.new
@zxcvbn.test(password, user_inputs)
end
end
extend self

def zxcvbn(password, user_inputs = [])
self.class.zxcvbn(password, user_inputs)
def test(password, user_inputs = [])
@zxcvbn = PasswordStrength.new
@zxcvbn.test(password, user_inputs)
end
end
23 changes: 23 additions & 0 deletions lib/zxcvbn/dictionary_ranker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# encoding: utf-8

module Zxcvbn
class DictionaryRanker
def self.rank_dictionaries(lists)
dictionaries = {}
lists.each do |dict_name, words|
dictionaries[dict_name] = rank_dictionary(words)
end
dictionaries
end

def self.rank_dictionary(words)
dictionary = {}
i = 1
words.each do |word|
dictionary[word] = i
i += 1
end
dictionary
end
end
end
25 changes: 3 additions & 22 deletions lib/zxcvbn/omnimatch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ class Omnimatch
DATA_PATH = Pathname(File.expand_path('../../../data', __FILE__))
ADJACENCY_GRAPHS = JSON.load(DATA_PATH.join('adjacency_graphs.json').read)
FREQUENCY_LISTS = YAML.load(DATA_PATH.join('frequency_lists.yaml').read)

attr_reader :ranked_dictionaries
RANKED_DICTIONARIES = DictionaryRanker.rank_dictionaries(FREQUENCY_LISTS)

def initialize
@ranked_dictionaries = rank_dictionaries
@matchers = build_matchers
end

Expand All @@ -27,33 +25,16 @@ def matches(password, user_inputs = [])

def user_input_matchers(user_inputs)
return [] unless user_inputs.any?
user_ranked_dictionary = rank_dictionary(user_inputs)
user_ranked_dictionary = DictionaryRanker.rank_dictionary(user_inputs)
dictionary_matcher = Matchers::Dictionary.new('user_inputs', user_ranked_dictionary)
l33t_matcher = Matchers::L33t.new([dictionary_matcher])
[dictionary_matcher, l33t_matcher]
end

def rank_dictionaries
dictionaries = {}
FREQUENCY_LISTS.each do |dict_name, words|
dictionaries[dict_name] = rank_dictionary(words)
end
dictionaries
end

def rank_dictionary(words)
dictionary = {}
i = 1
words.each do |word|
dictionary[word] = i
i += 1
end
dictionary
end

def build_matchers
matchers = []
dictionary_matchers = @ranked_dictionaries.map do |name, dictionary|
dictionary_matchers = RANKED_DICTIONARIES.map do |name, dictionary|
Matchers::Dictionary.new(name, dictionary)
end
l33t_matcher = Matchers::L33t.new(dictionary_matchers)
Expand Down
2 changes: 1 addition & 1 deletion spec/matchers/dictionary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Zxcvbn::Matchers::Dictionary do
let(:matcher) { described_class.new('english', dictionary) }
let(:dictionary) { RANKED_DICTIONARIES['english'] }
let(:dictionary) { Zxcvbn::Omnimatch::RANKED_DICTIONARIES['english'] }

it 'finds all the matches' do
matches = matcher.matches('whatisinit')
Expand Down
2 changes: 1 addition & 1 deletion spec/matchers/l33t_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe Zxcvbn::Matchers::L33t do
let(:matcher) { described_class.new([dictionary_matcher]) }
let(:dictionary) { RANKED_DICTIONARIES['english'] }
let(:dictionary) { Zxcvbn::Omnimatch::RANKED_DICTIONARIES['english'] }
let(:dictionary_matcher) { Zxcvbn::Matchers::Dictionary.new('english', dictionary) }

describe '#relevant_l33t_substitutions' do
Expand Down
4 changes: 1 addition & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,4 @@
'eheuczkqyq',
'rWibMFACxAUGZmxhVncy',
'Ba9ZyWABu99[BK#6MBgbH88Tofv)vs$w'
]

RANKED_DICTIONARIES = Zxcvbn::Omnimatch.new.ranked_dictionaries
]
10 changes: 4 additions & 6 deletions spec/zxcvbn_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require 'spec_helper'

describe 'zxcvbn over a set of example passwords' do
include Zxcvbn

TEST_PASSWORDS.each do |password|
it "gives back the same score for #{password}" do
ruby_result = zxcvbn(password)
ruby_result = Zxcvbn.test(password)
js_result = js_zxcvbn(password)

ruby_result.calc_time.should_not be_nil
Expand All @@ -21,21 +19,21 @@

context 'with a custom user dictionary' do
it 'scores them against the user dictionary' do
result = zxcvbn('themeforest', ['themeforest'])
result = Zxcvbn.test('themeforest', ['themeforest'])
result.entropy.should eq 0
result.score.should eq 0
end

it 'matches l33t substitutions on this dictionary' do
result = zxcvbn('th3m3for3st', ['themeforest'])
result = Zxcvbn.test('th3m3for3st', ['themeforest'])
result.entropy.should eq 1
result.score.should eq 0
end
end

context 'nil password' do
specify do
expect { zxcvbn(nil) }.to_not raise_error
expect { Zxcvbn.test(nil) }.to_not raise_error
end
end
end

0 comments on commit 744ba9d

Please sign in to comment.