1 # 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.
2 module Puppet::Parser::Functions
3 newfunction(:base64, :type => :rvalue, :doc => <<-'DOC') do |args|
4 Base64 encode or decode a string based on the command and the string submitted
8 $encodestring = base64('encode', 'thestring')
9 $decodestring = base64('decode', 'dGhlc3RyaW5n')
11 # explicitly define encode/decode method: default, strict, urlsafe
13 $encodestring = base64('encode', 'thestring', $method)
14 $decodestring = base64('decode', 'dGhlc3RyaW5n', $method)
16 Note: Since Puppet 4.8.0, the Binary data type can be used to produce base 64 encoded strings.
17 See the `new()` function for the Binary and String types for documentation. Also see `binary_file()`
18 function for reading a file with binary (non UTF-8) content.
20 # encode a string as if it was binary
21 $encodestring = String(Binary('thestring', '%s'))
22 # decode a Binary assuming it is an UTF-8 String
23 $decodestring = String(Binary("dGhlc3RyaW5n"), "%s")
29 raise Puppet::ParseError, "base64(): Wrong number of arguments (#{args.length}; must be >= 2)" unless args.length >= 2
31 actions = ['encode', 'decode']
33 unless actions.include?(args[0])
34 raise Puppet::ParseError, "base64(): the first argument must be one of 'encode' or 'decode'"
37 unless args[1].is_a?(String)
38 raise Puppet::ParseError, 'base64(): the second argument must be a string to base64'
41 method = ['default', 'strict', 'urlsafe']
43 chosen_method = if args.length <= 2
49 unless method.include?(chosen_method)
50 raise Puppet::ParseError, "base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'"
57 result = Base64.encode64(args[1])
59 result = Base64.strict_encode64(args[1])
61 result = Base64.urlsafe_encode64(args[1])
66 result = Base64.decode64(args[1])
68 result = Base64.strict_decode64(args[1])
70 result = Base64.urlsafe_decode64(args[1])