module Base64

def decode64(str)


Base64.decode64("MDEyMzQ1Njc==") # => "01234567"
Base64.decode64("MDEyMzQ1Njc=") # => "01234567"
Base64.decode64("MDEyMzQ1Njc") # => "01234567"

Padding in +str+ (even if incorrect) is ignored:

Base64.decode64("\x00\n-_") # => ""

these include newline characters and characters - and /:
see {Encoding Character Set}[Base64.html#module-Base64-label-Encoding+Character+Sets] above:
Non-\Base64 characters in +str+ are ignored;

Base64.decode64(s) # => "This is line 1\nThis is line 2\n"
s = "VGhpcyBpcyBsaW5lIDEKVGhpcyBpcyBsaW5lIDIK\n"

\Base64-encoded string +str+:
Returns a string containing the decoding of an RFC-2045-compliant
def decode64(str)
  str.unpack1("m")
end

def encode64(bin)


Base64.encode64(s) # => "VGhpcyBpcyBsaW5lIDEKVGhpcyBpcyBsaW5lIDIK\n"
s = "This is line 1\nThis is line 2\n"
Base64.encode64("\n\n\n") # => "CgoK\n"

which will be encoded as ordinary \Base64:
The string to be encoded may itself contain newlines,

# => "KioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq\nKg==\n"
Base64.encode64('*' * 46)
Base64.encode64('*') # => "Kg==\n"

see {Newlines}[Base64.html#module-Base64-label-Newlines] above:
will have one or more embedded newline characters;
The returned string ends with a newline character, and if sufficiently long

Base64.encode64('*') # => "Kg==\n"

see {Padding}[Base64.html#module-Base64-label-Padding] above.
The returned string may include padding;

Base64.encode64("\xFF\xFF\xFF") # => "////\n"
Base64.encode64("\xFB\xEF\xBE") # => "++++\n"

see {Encoding Character Set}[Base64.html#module-Base64-label-Encoding+Character+Sets] above:
+ or /;
Per RFC 2045, the returned string may contain the URL-unsafe characters

Returns a string containing the RFC-2045-compliant \Base64-encoding of +bin+.
def encode64(bin)
  [bin].pack("m")
end

def strict_decode64(str)


Base64.strict_decode64("MDEyMzQ1Njc==") # Raises ArgumentError
Base64.strict_decode64("MDEyMzQ1Njc=") # => "01234567"
Base64.strict_decode64("MDEyMzQ1Njc") # Raises ArgumentError

Padding in +str+, if present, must be correct:

Base64.strict_decode64('_') # Raises ArgumentError
Base64.strict_decode64('-') # Raises ArgumentError
Base64.strict_decode64("\n") # Raises ArgumentError

these include newline characters and characters - and /:
see {Encoding Character Set}[Base64.html#module-Base64-label-Encoding+Character+Sets] above:
Non-\Base64 characters in +str+ not allowed;

Base64.strict_decode64(s) # => "This is line 1\nThis is line 2\n"
s = "VGhpcyBpcyBsaW5lIDEKVGhpcyBpcyBsaW5lIDIK"

\Base64-encoded string +str+:
Returns a string containing the decoding of an RFC-2045-compliant
def strict_decode64(str)
  str.unpack1("m0")
end

def strict_encode64(bin)


Base64.strict_encode64(s) # => "VGhpcyBpcyBsaW5lIDEKVGhpcyBpcyBsaW5lIDIK"
s = "This is line 1\nThis is line 2\n"
Base64.strict_encode64("\n\n\n") # => "CgoK"

which will be encoded as ordinary \Base64:
The string to be encoded may itself contain newlines,

# => "KioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKg=="
Base64.strict_encode64('*' * 46)
Base64.strict_encode64('*') # => "Kg=="

see {Newlines}[Base64.html#module-Base64-label-Newlines] above:
The returned string will have no newline characters, regardless of its length;

Base64.strict_encode64('*') # => "Kg==\n"

see {Padding}[Base64.html#module-Base64-label-Padding] above.
The returned string may include padding;

Base64.strict_encode64("\xFF\xFF\xFF") # => "////\n"
Base64.strict_encode64("\xFB\xEF\xBE") # => "++++\n"

see {Encoding Character Set}[Base64.html#module-Base64-label-Encoding+Character+Sets] above:
+ or /;
Per RFC 2045, the returned string may contain the URL-unsafe characters

Returns a string containing the RFC-2045-compliant \Base64-encoding of +bin+.
def strict_encode64(bin)
  [bin].pack("m0")
end

def urlsafe_decode64(str)


Base64.urlsafe_decode64("MDEyMzQ1Njc==") # Raises ArgumentError.
Base64.urlsafe_decode64("MDEyMzQ1Njc=") # => "01234567"
Base64.urlsafe_decode64("MDEyMzQ1Njc") # => "01234567"

see {Padding}[Base64.html#module-Base64-label-Padding], above:
Padding in +str+, if present, must be correct:

Base64.urlsafe_decode64("\n") # Raises ArgumentError.
Base64.urlsafe_decode64('/') # Raises ArgumentError.
Base64.urlsafe_decode64('+') # Raises ArgumentError.

see {Encoding Character Set}[Base64.html#module-Base64-label-Encoding+Character+Sets] above:
+str+ may not contain non-Base64 characters;

Returns the decoding of an RFC-4648-compliant \Base64-encoded string +str+:
def urlsafe_decode64(str)
  # NOTE: RFC 4648 does say nothing about unpadded input, but says that
  # "the excess pad characters MAY also be ignored", so it is inferred that
  # unpadded input is also acceptable.
  if !str.end_with?("=") && str.length % 4 != 0
    str = str.ljust((str.length + 3) & ~3, "=")
    str.tr!("-_", "+/")
  else
    str = str.tr("-_", "+/")
  end
  strict_decode64(str)
end

def urlsafe_encode64(bin, padding: true)


# => "KioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKg=="
Base64.urlsafe_encode64('*' * 46)
Base64.urlsafe_encode64('*') # => "Kg=="

see {Newlines}[Base64.html#module-Base64-label-Newlines] above:
The returned string will have no newline characters, regardless of its length;

Base64.urlsafe_encode64('*', padding: false) # => "Kg"

Optionally, you can suppress padding:

Base64.urlsafe_encode64('*') # => "Kg=="

see {Padding}[Base64.html#module-Base64-label-Padding], above:
By default, the returned string may have padding;

Base64.urlsafe_encode64("\xFF\xFF\xFF") # => "____"
Base64.urlsafe_encode64("\xFB\xEF\xBE") # => "----"

see {Encoding Character Set}[Base64.html#module-Base64-label-Encoding+Character+Sets] above:
- and _;
but instead may contain the URL-safe characters
+ or /,
Per RFC 4648, the returned string will not contain the URL-unsafe characters

Returns the RFC-4648-compliant \Base64-encoding of +bin+.
def urlsafe_encode64(bin, padding: true)
  str = strict_encode64(bin)
  str.chomp!("==") or str.chomp!("=") unless padding
  str.tr!("+/", "-_")
  str
end