view modules/stdlib/lib/puppet/functions/seeded_rand_string.rb @ 275:d9352a684e62

Mass update of modules to remove deprecation warnings
author IBBoard <dev@ibboard.co.uk>
date Sun, 26 Jan 2020 11:36:07 +0000
parents
children adf6fe9bbc17
line wrap: on
line source

# @summary
#   Generates a consistent random string of specific length based on provided seed.
#
# @example Generate a consistently random string of length 8 with a seed:
#   seeded_rand_string(8, "${module_name}::redis_password")
#
# @example Generate a random string from a specific set of characters:
#   seeded_rand_string(5, '', 'abcdef')
Puppet::Functions.create_function(:seeded_rand_string) do
  # @param length Length of string to be generated.
  # @param seed Seed string.
  # @param charset String that contains characters to use for the random string.
  #
  # @return [String] Random string.
  dispatch :rand_string do
    param 'Integer[1]', :length
    param 'String', :seed
    optional_param 'String[2]', :charset
  end

  def rand_string(length, seed, charset = nil)
    require 'digest/sha2'

    charset ||= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

    random_generator = Random.new(Digest::SHA256.hexdigest(seed).to_i(16))

    Array.new(length) { charset[random_generator.rand(charset.size)] }.join
  end
end