Class: PasswordText
- Inherits:
-
String
- Object
- String
- PasswordText
- Defined in:
- lib/webget_ruby_password_text.rb
Constant Summary collapse
- @@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']
- @@length =
Default length
12
Class Method Summary collapse
-
.chars ⇒ Object
Get the default character array.
-
.chars=(chars) ⇒ Object
Set the default character array.
-
.length ⇒ Object
Get the default length.
-
.length=(length) ⇒ Object
Set the default length.
Instance Method Summary collapse
-
#initialize(length = @@length, chars = @@chars) ⇒ PasswordText
constructor
Return a new secure random password.
Constructor Details
#initialize(length = @@length, chars = @@chars) ⇒ 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
PasswordText.new => "avzwbnxremcd"
PasswordText.new(4) => "avzw"
PasswordText.new(4,['x','y','z']) => "yzyx"
64 65 66 |
# File 'lib/webget_ruby_password_text.rb', line 64 def initialize(length=@@length,chars=@@chars) super(Array.new(length){chars[SecureRandom.random_number(chars.size)]}.join) end |
Class 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.
77 78 79 |
# File 'lib/webget_ruby_password_text.rb', line 77 def self.chars @@chars end |
.chars=(chars) ⇒ Object
Set the default character array
84 85 86 |
# File 'lib/webget_ruby_password_text.rb', line 84 def self.chars=(chars) @@chars=chars end |
.length ⇒ 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.
94 95 96 |
# File 'lib/webget_ruby_password_text.rb', line 94 def self.length @@length||=12 end |
.length=(length) ⇒ Object
Set the default length
101 102 103 |
# File 'lib/webget_ruby_password_text.rb', line 101 def self.length=(length) @@length=length end |