Puppet Function: seeded_rand_string
- Defined in:
-
vendor_modules/stdlib/lib/puppet/functions/seeded_rand_string.rb
- Function type:
- Ruby 4.x API
Summary
Generates a consistent random string of specific length based on provided seed.
Overview
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'vendor_modules/stdlib/lib/puppet/functions/seeded_rand_string.rb', line 11
Puppet::Functions.create_function(:seeded_rand_string) do
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
|