]>
git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - modules/base_installation/lib/puppet/parser/functions/generate_password.rb
1 module Puppet
::Parser::Functions
2 newfunction(:generate_password, :type => :rvalue, :doc => <<-EOS
3 Returns a semi-random string based on a seed and a value. Will always generate the same value with the same entry.
5 generate_password(length, seed_file, password_key)
8 arguments
= arguments
.shift
if arguments
.first
.is_a
?(Array
)
10 raise Puppet
::ParseError, "generate_password(): Wrong number of arguments " +
11 "given (#{arguments.size} for 3)" if arguments
.size !
= 3
13 size
= arguments
.shift
14 seed_file
= arguments
.shift
15 password_key
= arguments
.shift
17 unless size
.class.ancestors
.include?(Numeric
) or size
.is_a
?(String
)
18 raise Puppet
::ParseError, 'generate_password(): Requires a numeric first argument'
23 set
= ('a' .. 'z').to_a +
('A' .. 'Z').to_a +
('0' .. '9').to_a
25 key
= "#{File.open(seed_file).read}:#{password_key}"
27 size
.times
.collect
do |i
|
28 set
[OpenSSL
::HMAC.hexdigest(OpenSSL
::Digest.new('sha256'), key
, i
.to_s
).to_i(16) % set
.size
]