Class: KBSecret::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/kbsecret/generator.rb

Overview

Generates secret values (passwords, environment keys, etc) for storage by KBSecret.

Constant Summary collapse

GENERATOR_TYPES =

All generator formats known by KBSecret::Generator.

%i[hex base64].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile = :default) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • profile (Symbol, String) (defaults to: :default)

    the label of the generator profile to use

Raises:



20
21
22
23
24
25
26
# File 'lib/kbsecret/generator.rb', line 20

def initialize(profile = :default)
  @format = Config.generator(profile)[:format].to_sym
  @length = Config.generator(profile)[:length].to_i

  raise Exceptions::GeneratorLengthError, @length unless @length.positive?
  raise Exceptions::GeneratorFormatError, @format unless GENERATOR_TYPES.include?(@format)
end

Instance Attribute Details

#formatSymbol (readonly)

Returns the format of the generator.

Returns:

  • (Symbol)

    the format of the generator



12
13
14
# File 'lib/kbsecret/generator.rb', line 12

def format
  @format
end

#lengthInteger (readonly)

Returns the length, in bytes of secrets generated by the generator.

Returns:

  • (Integer)

    the length, in bytes of secrets generated by the generator



15
16
17
# File 'lib/kbsecret/generator.rb', line 15

def length
  @length
end

Instance Method Details

#secretString

Returns a new secret based on the #format and #length of the KBSecret::Generator.

Examples:

g = KBSecret::Generator.new # => #<KBSecret::Generator @format="hex", @length=16>
g.secret # => "a927f1e7ffa1a039a9cd31c45bc181e3"

Returns:



32
33
34
# File 'lib/kbsecret/generator.rb', line 32

def secret
  SecureRandom.send(@format, @length)
end