]>
Commit | Line | Data |
---|---|---|
54806111 IB |
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. | |
4 | Prototype: | |
5 | generate_password(length, seed_file, password_key) | |
6 | EOS | |
7 | ) do |*arguments| | |
8 | arguments = arguments.shift if arguments.first.is_a?(Array) | |
9 | ||
10 | raise Puppet::ParseError, "generate_password(): Wrong number of arguments " + | |
11 | "given (#{arguments.size} for 3)" if arguments.size != 3 | |
12 | ||
13 | size = arguments.shift | |
14 | seed_file = arguments.shift | |
15 | password_key = arguments.shift | |
16 | ||
17 | unless size.class.ancestors.include?(Numeric) or size.is_a?(String) | |
18 | raise Puppet::ParseError, 'generate_password(): Requires a numeric first argument' | |
19 | end | |
20 | ||
21 | size = size.to_i | |
22 | ||
23 | set = ('a' .. 'z').to_a + ('A' .. 'Z').to_a + ('0' .. '9').to_a | |
24 | ||
25 | key = "#{File.open(seed_file).read}:#{password_key}" | |
26 | ||
27 | size.times.collect do |i| | |
28 | set[OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, i.to_s).to_i(16) % set.size] | |
29 | end.join | |
30 | end | |
31 | end |