diff options
author | mh <mh@immerda.ch> | 2014-06-27 19:23:20 +0200 |
---|---|---|
committer | mh <mh@immerda.ch> | 2014-06-27 19:23:20 +0200 |
commit | ecb2a2c7c6ec3576bc49747a484fa0f8e93a50fa (patch) | |
tree | 05ccf120e136b3d6d77a8d91f9930f0139b8e54b /lib/trocla/util.rb | |
parent | 20de208ee827cb451e60705180909ce81eae0127 (diff) | |
parent | 08ac533d2156b666ae6ca68e797992629051315f (diff) |
Merge branch 'tilya-charset_option'
Diffstat (limited to 'lib/trocla/util.rb')
-rw-r--r-- | lib/trocla/util.rb | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/lib/trocla/util.rb b/lib/trocla/util.rb index ff7e3ce..78462f5 100644 --- a/lib/trocla/util.rb +++ b/lib/trocla/util.rb @@ -2,30 +2,36 @@ require 'securerandom' class Trocla class Util class << self - def random_str(length=12,shellsafe=:undef) - if shellsafe - (1..length).collect{|a| safechars[SecureRandom.random_number(safechars.size)] }.join.to_s - else - (1..length).collect{|a| chars[SecureRandom.random_number(chars.size)] }.join.to_s - end + def random_str(length=12, charset='default') + _charsets = charsets[charset] + (1..length).collect{|a| _charsets[SecureRandom.random_number(_charsets.size)] }.join.to_s end def salt(length=8) - (1..length).collect{|a| normal_chars[SecureRandom.random_number(normal_chars.size)] }.join.to_s + (1..length).collect{|a| alphanumeric[SecureRandom.random_number(alphanumeric.size)] }.join.to_s end private + + def charsets + @charsets ||= { + 'default' => chars, + 'alphanumeric' => alphanumeric, + 'shellsafe' => shellsafe, + } + end + def chars - @chars ||= normal_chars + special_chars + @chars ||= shellsafe + special_chars end - def safechars - @chars ||= normal_chars + shellsafe_chars + def shellsafe + @chars ||= alphanumeric + shellsafe_chars end - def normal_chars - @normal_chars ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + def alphanumeric + @alphanumeric ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a end def special_chars - @special_chars ||= "+*%/()@&=?![]{}-_.,;:".split(//) + @special_chars ||= "*()&![]{}-".split(//) end def shellsafe_chars @shellsafe_chars ||= "+%/@=?_.,:".split(//) |