2 # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
4 module Puppet::Parser::Functions
6 newfunction(:base64, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
8 Base64 encode or decode a string based on the command and the string submitted
12 $encodestring = base64('encode', 'thestring')
13 $decodestring = base64('decode', 'dGhlc3RyaW5n')
15 # explicitly define encode/decode method: default, strict, urlsafe
17 $encodestring = base64('encode', 'thestring', $method)
18 $decodestring = base64('decode', 'dGhlc3RyaW5n', $method)
24 raise Puppet::ParseError, ("base64(): Wrong number of arguments (#{args.length}; must be >= 2)") unless args.length >= 2
26 actions = ['encode','decode']
28 unless actions.include?(args[0])
29 raise Puppet::ParseError, ("base64(): the first argument must be one of 'encode' or 'decode'")
32 unless args[1].is_a?(String)
33 raise Puppet::ParseError, ("base64(): the second argument must be a string to base64")
36 method = ['default','strict','urlsafe']
39 chosenMethod = 'default'
41 chosenMethod = args[2]
44 unless method.include?(chosenMethod)
45 raise Puppet::ParseError, ("base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'")
52 result = Base64.encode64(args[1])
54 result = Base64.strict_encode64(args[1])
56 result = Base64.urlsafe_encode64(args[1])
61 result = Base64.decode64(args[1])
63 result = Base64.strict_decode64(args[1])
65 result = Base64.urlsafe_decode64(args[1])