class KBSecret::Generator

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

Constants

GENERATOR_TYPES

All generator formats known by {Generator}.

Attributes

format[R]

@return [Symbol] the format of the generator

length[R]

@return [Integer] the length, in bytes of secrets generated by the generator

Public Class Methods

new(profile = :default) click to toggle source

@param profile [Symbol, String] the label of the generator profile to use @raise [Exceptions::GeneratorLengthError] if the profile has a non-positive length @raise [Exceptions::GeneratorFormatError] if the profile has an unknown format

# 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

Public Instance Methods

secret() click to toggle source

@return [String] a new secret based on the {format} and {length} of the {Generator} @example

g = KBSecret::Generator.new # => #<KBSecret::Generator @format="hex", @length=16>
g.secret # => "a927f1e7ffa1a039a9cd31c45bc181e3"
# File lib/kbsecret/generator.rb, line 32
def secret
  SecureRandom.send(@format, @length)
end