Class: PasswordText
- Inherits:
-
String
- Object
- String
- PasswordText
- Defined in:
- lib/sixarm_ruby_password_text.rb
Constant Summary
- 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)
-
- (Object) chars
Get the default character array.
-
- (Object) chars=(chars)
Set the default character array.
-
- (PasswordText) initialize(opts = {})
constructor
Return a new secure random password.
-
- (Object) len
Get the default length.
-
- (Object) len=(length)
Set the default length.
-
- (Object) next
Return the next random password of the same length and character array.
Constructor Details
- (PasswordText) initialize(opts = {})
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
- (Object) chars
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 |
- (Object) chars=(chars)
Set the default character array
65 66 67 |
# File 'lib/sixarm_ruby_password_text.rb', line 65 def chars=(chars) @chars=chars end |
- (Object) len
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 |
- (Object) len=(length)
Set the default length
82 83 84 |
# File 'lib/sixarm_ruby_password_text.rb', line 82 def len=(length) @len=length end |
- (Object) next
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 |