summaryrefslogtreecommitdiff
path: root/lib/trocla/util.rb
blob: 2b1c6c685456d822c7a662f4b71c18d3ff76c9c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require 'securerandom'
class Trocla
  class Util
    class << self
      def random_str(length=12)
        (1..length).collect{|a| chars[SecureRandom.random_number(chars.size)] }.join.to_s
      end

      def salt(length=8)
        (1..length).collect{|a| normal_chars[SecureRandom.random_number(normal_chars.size)] }.join.to_s
      end

      private
      def chars
        @chars ||= normal_chars + special_chars
      end
      def normal_chars
        @normal_chars ||= ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
      end
      def special_chars
        @special_chars ||= "+*%/()@&=?![]{}-_.,;:".split(//)
      end
    end
  end
end