Class: SecureRandomString
- Inherits:
-
String
- Object
- String
- SecureRandomString
- Defined in:
- lib/secure_random_string.rb
Constant Summary collapse
- UC_LETTERS =
('A'..'Z').to_a.freeze
- LC_LETTERS =
('a'..'z').to_a.freeze
- NUMBERS =
(0..9).to_a.freeze
- CHARACTERS =
%w{/ . , + ^ [ ] - + - = _ * % @ : # ~}.freeze
Instance Method Summary collapse
-
#initialize(length, options = {}) ⇒ SecureRandomString
constructor
A new instance of SecureRandomString.
Constructor Details
#initialize(length, options = {}) ⇒ SecureRandomString
Returns a new instance of SecureRandomString.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/secure_random_string.rb', line 10 def initialize(length, = {}) # By default, don't include characters in strings. [:characters] ||= false # Build a set of possible characters for the string set = [] set += UC_LETTERS unless [:uppercase] == false set += LC_LETTERS unless [:lowercase] == false set += NUMBERS unless [:numbers] == false set += CHARACTERS unless [:characters] == false set += [:extra] if [:extra].is_a?(Array) # Populate the string length.times.each do self << set[SecureRandom.random_number(set.size)].to_s end end |