Class: CipherBureau::Password

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

Overview

Copyright © 2013 Dynamic Project Management AS Copyright © 2013 Knut I. Stenmark

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Constant Summary collapse

NUMBERS =
('0'..'9').to_a
RANDOM =
('!'..'~').to_a
LETTERS_AND_NUMBERS =
('a'..'z').to_a + ('A'..'Z').to_a + NUMBERS
SYMBOLS_AND_NUMBERS =
RANDOM - ('a'..'z').to_a - ('A'..'Z').to_a

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Password

Returns a new instance of Password.

Raises:



68
69
70
71
72
73
74
# File 'lib/cipher_bureau/password.rb', line 68

def initialize(options = {})
  @options = options.reverse_merge(CipherBureau.default_memorable_options)
  @options.assert_valid_keys(:length, :country_code, :ascii, :grammar, :name_type, :camelize, :middle_numbers)
  @length = @options.delete(:length) || CipherBureau.password_length
  raise ArgumentError, 'Password length is undefined.' unless @length
  @options.delete(:name_type) unless @options[:grammar] == 'name'
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



24
25
26
# File 'lib/cipher_bureau/password.rb', line 24

def length
  @length
end

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/cipher_bureau/password.rb', line 24

def options
  @options
end

Class Method Details

.letters_and_numbers(*args) ⇒ Object



31
32
33
# File 'lib/cipher_bureau/password.rb', line 31

def letters_and_numbers(*args)
  enum :letters_and_numbers, *args
end

.memorable(*args) ⇒ Object



43
44
45
# File 'lib/cipher_bureau/password.rb', line 43

def memorable(*args)
  enum :memorable, *args
end

.numbers_only(*args) ⇒ Object



35
36
37
# File 'lib/cipher_bureau/password.rb', line 35

def numbers_only(*args)
  enum :numbers_only, *args
end

.random(*args) ⇒ Object



27
28
29
# File 'lib/cipher_bureau/password.rb', line 27

def random(*args)
  enum :random, *args
end

.strength(password) ⇒ Object



47
48
49
# File 'lib/cipher_bureau/password.rb', line 47

def strength(password)
  CipherBureau::PasswordMeter.strength(password)
end

.symbols_and_numbers(*args) ⇒ Object



39
40
41
# File 'lib/cipher_bureau/password.rb', line 39

def symbols_and_numbers(*args)
  enum :symbols_and_numbers, *args
end

Instance Method Details

#letters_and_numbers(size = length) ⇒ Object



86
87
88
# File 'lib/cipher_bureau/password.rb', line 86

def letters_and_numbers(size = length)
  (0...size).map{LETTERS_AND_NUMBERS.sample}.join
end

#memorableObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/cipher_bureau/password.rb', line 94

def memorable()
  sym_size = length / 4   
  end_size = (length - sym_size) / 2
  beg_size = length - end_size - sym_size
  # puts "beg_size: #{beg_size} sym_size: #{sym_size} end_size: #{end_size}"
  beg_word = CipherBureau::Dictionary.random(beg_size, scope)
  end_word = CipherBureau::Dictionary.random(end_size, scope)
  sym_word = options[:middle_numbers] ? numbers_only(sym_size) : symbols_and_numbers(sym_size)
  "#{beg_word}#{sym_word}#{end_word}"
end

#numbers_only(size = length) ⇒ Object



90
91
92
# File 'lib/cipher_bureau/password.rb', line 90

def numbers_only(size = length)
  (0...size).map{NUMBERS.sample}.join
end

#random(size = length) ⇒ Object



81
82
83
# File 'lib/cipher_bureau/password.rb', line 81

def random(size = length)
  (0...size).map{RANDOM.sample}.join
end

#symbols_and_numbers(size = length) ⇒ Object



105
106
107
# File 'lib/cipher_bureau/password.rb', line 105

def symbols_and_numbers(size = length)
  (0...size).map{SYMBOLS_AND_NUMBERS.sample}.join
end