Class: PasswordText
- Inherits:
-
String
- Object
- String
- PasswordText
- Defined in:
- lib/sixarm_ruby_password_text.rb
Constant Summary collapse
- DEFAULT_CHARS =
Default characters
['a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z']
- DEFAULT_LEN =
Default length
12
Instance Method Summary collapse
-
#chars ⇒ Object
Get the default character array.
-
#chars=(chars) ⇒ Object
Set the default character array.
-
#initialize(opts = {}) ⇒ PasswordText
constructor
Return a new secure random password.
-
#len ⇒ Object
Get the default length.
-
#len=(length) ⇒ Object
Set the default length.
-
#next ⇒ Object
Return the next random password of the same length and character array.
Constructor Details
#initialize(opts = {}) ⇒ PasswordText
Return a new secure random password.
The password has a given length (or the default) and is picked from a given character array (or the default).
To set the default length, see #length
To set the default character array, see #chars
Examples
password = PasswordText.new => "avzwbnxremcd"
password = PasswordText.new(4) => "avzw"
password = PasswordText.new(4,['x','y','z']) => "yzyx"
42 43 44 45 46 |
# File 'lib/sixarm_ruby_password_text.rb', line 42 def initialize(opts={}) self.chars= opts[:chars] || DEFAULT_CHARS self.len= opts[:len] || DEFAULT_LEN super(Array.new(len){chars[SecureRandom.random_number(chars.size)]}.join) end |
Instance Method Details
#chars ⇒ Object
Get the default character array.
To improve usability, the passwords only use lowercase letters. This helps people who use mobile phones and also helps people who have some kinds disabilities related to manual dexterity. We also omit letters that may be confused with numbers: “i”, “l”, “o”.
We choose this as a valuable tradeoff between usability and complexity.
58 59 60 |
# File 'lib/sixarm_ruby_password_text.rb', line 58 def chars @chars || DEFAULT_CHARS end |
#chars=(chars) ⇒ Object
Set the default character array
65 66 67 |
# File 'lib/sixarm_ruby_password_text.rb', line 65 def chars=(chars) @chars=chars end |
#len ⇒ Object
Get the default length
We choose 12 characters to make a sufficiently strong password. for usual web applications. You can make this stronger as needed.
75 76 77 |
# File 'lib/sixarm_ruby_password_text.rb', line 75 def len @len || DEFAULT_LEN end |
#len=(length) ⇒ Object
Set the default length
82 83 84 |
# File 'lib/sixarm_ruby_password_text.rb', line 82 def len=(length) @len=length end |
#next ⇒ Object
Return the next random password of the same length and character array
89 90 91 |
# File 'lib/sixarm_ruby_password_text.rb', line 89 def next PasswordText.new(:chars => chars, :len => len) end |