Class: Sifar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSifar

Returns a new instance of Sifar.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sifar.rb', line 7

def initialize
    @checks = {
        'L' => :check_length,
        'D' => :check_dictionary,
        'H' => :check_heterogeneity,
        'W' => :check_word_blacklist,
        'C' => :check_char_blacklist,
        'P' => :check_phonetic,
        'T' => :check_similarity,
        'S' => :check_string
    }
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/sifar.rb', line 5

def errors
  @errors
end

Instance Method Details

#check_against_name(password, name) ⇒ Object



38
39
40
# File 'lib/sifar.rb', line 38

def check_against_name(password, name)
    @options[:name_check_type].each(''){|option| self.send(@checks[option], password, name)}
end

#check_char_blacklist(word) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sifar.rb', line 81

def check_char_blacklist(word)
    if File.readable? @options[:char_blacklist]
        File.open(@options[:char_blacklist]) do |f|
            f.readlines.each do |char|
                char = char.strip
                if not(char.empty?) and word.include?(char)
                    @errors << @error_messages[:blackchar]
                    return false
                end
            end
        end
    else
        @errors << @error_messages[:config_blackchar]
        return false
    end
    true
end

#check_dictionary(word) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/sifar.rb', line 65

def check_dictionary(word)
    if File.readable? @options[:dictionary]
        @errors << @error_messages[:dict] if find_word_in_file(word, @options[:dictionary])
    else
        @errors << @error_messages[:config_blackword]
    end
end

#check_heterogeneity(word) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sifar.rb', line 46

def check_heterogeneity(word)
    num_chars = word.length
    num = {}
    num[:upper] = word.gsub(/[^[:upper:]]/, '').length
    num[:lower] = word.gsub(/[^[:lower:]]/, '').length
    num[:digit] = word.gsub(/[^[:digit:]]/, '').length
    num[:special] = num_chars - (num[:upper] + num[:lower] + num[:digit])

    max = 4 > num_chars ? 3 : num_chars - 3
    num.each do |type, value|
        if value > max
            @errors << @error_messages[:heter]
            return false
        end
    end

    true
end

#check_length(word) ⇒ Object



42
43
44
# File 'lib/sifar.rb', line 42

def check_length(word)
    @errors << @error_messages[:length] if word.length < @options[:minimum_length]
end

#check_password(password, name = '', options = {}, error_messages = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/sifar.rb', line 20

def check_password(password, name = '', options = {}, error_messages = {})
    self.set_opt options, error_messages
    @errors = []

    self.check_strength(password)
    self.check_against_name(password, name) unless name.empty?

    1 > @errors.length
end

#check_phonetic(password, name) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/sifar.rb', line 99

def check_phonetic(password, name)
    require 'english/levenshtein'
    require 'english/soundex'
    unless 2 < English::Levenshtein.distance(password.soundex, name.soundex)
        @errors << @error_messages[:phonetic]
        return false
    end
    true
end

#check_similarity(password, name) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/sifar.rb', line 109

def check_similarity(password, name)
    require 'english/similarity'
    if 0.8 < password.similarity(name)
        @errors << @error_messages[:similar]
        return false
    end
    true
end

#check_strength(password) ⇒ Object



34
35
36
# File 'lib/sifar.rb', line 34

def check_strength(password)
    @options[:password_check_type].each(''){|option| self.send(@checks[option], password)}
end

#check_string(password, name) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/sifar.rb', line 118

def check_string(password, name)
    longer, shorter = password.size > name.size ? [password, name] : [name, password]
    if longer.include?(shorter)
        @errors << @error_messages[:similar]
        return false
    end
    true
end

#check_word_blacklist(word) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/sifar.rb', line 73

def check_word_blacklist(word)
    if File.readable? @options[:word_blacklist]
        @errors << @error_messages[:blackword] if find_word_in_file(word, @options[:word_blacklist])
    else
        @errors << @error_messages[:config_dict]
    end
end

#find_word_in_file(word, file) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/sifar.rb', line 127

def find_word_in_file(word, file)
    t = []
    IO.popen("#{@options[:grep_path]} -ixs '#{word}' #{file}") {|io|
        r = io.readlines
        t << r unless r.empty?
    }
    not t.empty?
end

#generate_password(name = '', options = {}, error_messages = {}) ⇒ Object



30
31
32
# File 'lib/sifar.rb', line 30

def generate_password(name = '', options = {}, error_messages = {})
    self.set_opt options, error_messages
end

#set_opt(options = {}, error_messages = {}) ⇒ Object

Raises:

  • (ArgumentError)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sifar.rb', line 136

def set_opt(options = {}, error_messages = {})
    @options = {
        :minimum_length => 8,
        :password_check_type => 'LD',
        :name_check_type => 'S',
        :dictionary => '',
        :word_blacklist => '',
        :char_blacklist => '',
        :error_msg_hash => {},
        :umlaut_hash => {},
        :grep_path => '/bin/grep'
    }.merge options
    @options[:minimum_length] = 1 > @options[:minimum_length] ? 8 : @options[:minimum_length]
    @options[:password_check_type] = @options[:password_check_type].gsub(/[^DLHWC]/, '').split('').sort.join
    @options[:name_check_type] = @options[:name_check_type].gsub(/[^PTS]/, '').split('').sort.join

    raise ArgumentError, 'grep not found' unless (File.stat(@options[:grep_path]).executable? rescue false)

    @error_messages = {
        :length => 'The password is too short!',
        :similar => 'The password is too similar to the given name!',
        :phonetic => 'The password sounds too similar to the given name!',
        :dict => 'The password is based upon a dictionary word!',
        :heter => 'The password is too homogeneous!',
        :blackword => 'The password is based upon a blacklsited word!',
        :blackchar => 'The password contains a blacklisted character!',
        :similar_looking_chars => 'The password contains  similar looking  characters!',
        :config_blackchar => 'Could not load a valid blacklisted character file!',
        :config_blackword => 'Could not load a valid blacklisted word file!',
        :config_dict => 'Could not load a valid dictionary file!',
        :config_heter => 'Conflict in heterogeneity check and password generation configuration!',
        :config_length => 'Generated password length cannot be less then specified minimum length!'
    }.merge error_messages
end