Class: Locksmith
- Inherits:
-
Object
- Object
- Locksmith
- Defined in:
- lib/locksmith.rb
Constant Summary collapse
- @@alpha_hex =
Tuwaga.new(16, 'abcdefghijklmnop')
- @@symbol_hex =
Tuwaga.new(16, '~!@#$%^&*()_+-={')
- @@hex =
Tuwaga.new(16)
Instance Attribute Summary collapse
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#max_length ⇒ Object
Returns the value of attribute max_length.
-
#private_password ⇒ Object
Returns the value of attribute private_password.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #generated_password ⇒ Object
-
#initialize(private_password, domain, username = '', options = {}) ⇒ Locksmith
constructor
A new instance of Locksmith.
- #use_alphabet=(use_alphabet) ⇒ Object
- #use_alphabet? ⇒ Boolean
- #use_number=(use_number) ⇒ Object
- #use_number? ⇒ Boolean
- #use_symbol=(use_symbol) ⇒ Object
- #use_symbol? ⇒ Boolean
- #valid? ⇒ Boolean
Constructor Details
#initialize(private_password, domain, username = '', options = {}) ⇒ Locksmith
Returns a new instance of Locksmith.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/locksmith.rb', line 13 def initialize private_password, domain, username = '', = {} @private_password = private_password @domain = domain @username = username @use_alphabet = .has_key?(:use_alphabet) ? [:use_alphabet] : true @use_number = .has_key?(:use_number) ? [:use_number] : true @use_symbol = .has_key?(:use_symbol) ? [:use_symbol] : true @max_length = .has_key?(:max_length) ? [:max_length] : nil @errors = [] end |
Instance Attribute Details
#domain ⇒ Object
Returns the value of attribute domain.
6 7 8 |
# File 'lib/locksmith.rb', line 6 def domain @domain end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/locksmith.rb', line 7 def errors @errors end |
#max_length ⇒ Object
Returns the value of attribute max_length.
6 7 8 |
# File 'lib/locksmith.rb', line 6 def max_length @max_length end |
#private_password ⇒ Object
Returns the value of attribute private_password.
6 7 8 |
# File 'lib/locksmith.rb', line 6 def private_password @private_password end |
#username ⇒ Object
Returns the value of attribute username.
6 7 8 |
# File 'lib/locksmith.rb', line 6 def username @username end |
Instance Method Details
#generated_password ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/locksmith.rb', line 67 def generated_password if valid? raw = @private_password + @domain + (@username || '') hex_string = Digest::MD5.hexdigest(raw) result = '' if @use_alphabet result += @@alpha_hex.convert_from(hex_string[0, 1], @@hex).upcase hex_string = hex_string[1, (hex_string.length - 1)] result += @@alpha_hex.convert_from(hex_string[0, 1], @@hex).downcase hex_string = hex_string[1, (hex_string.length - 1)] end if @use_number result += @@hex.to_decimal(hex_string[0, 1]).to_s hex_string = hex_string[1, (hex_string.length - 1)] end if @use_symbol result += @@symbol_hex.convert_from(hex_string[0, 1], @@hex) hex_string = hex_string[1, (hex_string.length - 1)] end base_x_symbols = '' base_x_symbols += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' if @use_alphabet base_x_symbols += '0123456789' if @use_number base_x_symbols += '~!@#$%^&*()_+-={}|[]\\;\':"<>?,./' if @use_symbol base_x = Tuwaga.new(base_x_symbols.length, base_x_symbols) result += base_x.convert_from(hex_string, @@hex) if @max_length result = result[0,@max_length] end result else raise('Can not generate password: ' + @errors.join(', ')) end end |
#use_alphabet=(use_alphabet) ⇒ Object
29 30 31 |
# File 'lib/locksmith.rb', line 29 def use_alphabet= use_alphabet @use_alphabet = use_alphabet end |
#use_alphabet? ⇒ Boolean
25 26 27 |
# File 'lib/locksmith.rb', line 25 def use_alphabet? @use_alphabet end |
#use_number=(use_number) ⇒ Object
37 38 39 |
# File 'lib/locksmith.rb', line 37 def use_number= use_number @use_number = use_number end |
#use_number? ⇒ Boolean
33 34 35 |
# File 'lib/locksmith.rb', line 33 def use_number? @use_number end |
#use_symbol=(use_symbol) ⇒ Object
45 46 47 |
# File 'lib/locksmith.rb', line 45 def use_symbol= use_symbol @use_symbol = use_symbol end |
#use_symbol? ⇒ Boolean
41 42 43 |
# File 'lib/locksmith.rb', line 41 def use_symbol? @use_symbol end |
#valid? ⇒ Boolean
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/locksmith.rb', line 49 def valid? @errors = [] if !@private_password || @private_password.empty? @errors << 'private password is required' end if !@domain || @domain.empty? @errors << 'domain is required' end if !@use_alphabet && !@use_number && !@use_symbol @errors << 'at least on of use_alphabet, use_number or use_symbol must be true' end (@errors.length == 0) end |