Merge branch 'charset_option' of https://github.com/tilya/trocla into tilya-charset_o...
authormh <mh@immerda.ch>
Fri, 27 Jun 2014 16:59:02 +0000 (18:59 +0200)
committermh <mh@immerda.ch>
Fri, 27 Jun 2014 16:59:02 +0000 (18:59 +0200)
& simplify charset selection

Conflicts:
lib/trocla.rb
lib/trocla/util.rb

1  2 
lib/trocla/default_config.yaml
lib/trocla/util.rb

@@@ -2,6 -2,6 +2,7 @@@
  options:
      random: true
      length: 12
++    charset: default
  adapter: :YAML
  adapter_options:
      :file: '/tmp/trocla.yaml'
@@@ -2,30 -2,32 +2,36 @@@ require 'securerandom
  class Trocla
    class Util
      class << self
-       def random_str(length=12,shellsafe=:undef)
-         if shellsafe
 -      def random_str(length=12, charset=:undef)
 -        if charset == 'alphanumeric'
 -          (1..length).collect{|a| normal_chars[SecureRandom.random_number(normal_chars.size)] }.join.to_s
 -        elsif charset == 'special_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(//)