Sha256: cf98217c786b2497b3b660a16bf3ed1e4473b050fb682b1c99ae49ee7e820d0c

Contents?: true

Size: 1.05 KB

Versions: 36

Compression:

Stored size: 1.05 KB

Contents

module KineticSdk
  module Utils

    # The Random module provides methods to generate random secrets of varying
    # complexity.
    module Random

      # Symbols that are allowed by default
      DEFAULT_SYMBOLS = %w( ! @ % * ( ) { } [ ] )
      # Symbols that are represented as 7-bit ASCII
      SEVEN_BIT_ASCII_SYMBOLS = %w(! " # $ % & ' ( ) * + - . / : ; < = > ? @ [ \ ] & _ ` { | } ~)
      # Alpha-numeric characters that are respresented as 7-bit ASCII
      SEVEN_BIT_ASCII_ALPHANUM = 
        ('A'..'Z').to_a.concat(
          ('a'..'z').to_a).concat(
            ('0'..'9').to_a)

      # Generates a simple secret based on ASCII alpha-numeric characters,
      # and a few standard symbol characters.
      #
      # @param size [Fixnum] The length of the generated secret
      # @param allowed_symbols [Array] symbols to allow
      def self.simple(size = 20, allowed_symbols = DEFAULT_SYMBOLS)
        chars = (allowed_symbols || Array.new).concat(SEVEN_BIT_ASCII_ALPHANUM)
        (0...size).map { chars[rand(chars.size)] }.join
      end

    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
kinetic_sdk-5.0.27 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.26 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.25 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.24 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.23 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.22 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.21 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.20 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.19 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.18 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.17 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.16 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.15 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.14 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.13 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.12 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.11 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.10 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.9 lib/kinetic_sdk/utils/random.rb
kinetic_sdk-5.0.8 lib/kinetic_sdk/utils/random.rb