Module: Base64

Defined in:
lib/ronin/formatting/extensions/binary/base64.rb

Overview

Adds Ruby 1.9 specific methods when running on Ruby 1.8.7.

Class Method Summary collapse

Class Method Details

.strict_decode64(str) ⇒ String

Note:

This method complies with RFC 4648.

Decodes a strictly base64 encoded String.

Parameters:

  • str (String)

    The strictly encoded base64 String.

Returns:

  • (String)

    The decoded String.

Raises:

  • (ArgumentError)

    The String is incorrectly padded or contains non-alphabet characters. Note: CR or LF are also rejected.


62
63
64
65
66
67
68
# File 'lib/ronin/formatting/extensions/binary/base64.rb', line 62

def strict_decode64(str)
  unless str.include?("\n")
    decode64(str)
  else
    raise(ArgumentError,"invalid base64")
  end
end

.strict_encode64(bin) ⇒ String

Note:

This method complies with RFC 4648. No line feeds are added.

Strictly encodes a base64 String.

Parameters:

  • bin (String)

    The String to encode.

Returns:

  • (String)

    The strictly encoded base64 String.


42
43
44
# File 'lib/ronin/formatting/extensions/binary/base64.rb', line 42

def strict_encode64(bin)
  encode64(bin).tr("\n",'')
end

.urlsafe_decode64(str) ⇒ String

Note:

This method complies with ``Base 64 Encoding with URL and filename Safe Alphabet'' in RFC 4648. The alphabet uses '-' instead of '+' and '_' instead of '/'.

Decodes a URL-safe base64 encoded String.

Parameters:

  • str (String)

    The URL-safe encoded base64 String.

Returns:

  • (String)

    The decoded String.


102
103
104
# File 'lib/ronin/formatting/extensions/binary/base64.rb', line 102

def urlsafe_decode64(str)
  strict_decode64(str.tr("-_", "+/"))
end

.urlsafe_encode64(bin) ⇒ String

Note:

This method complies with ``Base 64 Encoding with URL and filename Safe Alphabet'' in RFC 4648. The alphabet uses '-' instead of '+' and '_' instead of '/'.

Encodes a URL-safe base64 String.

Parameters:

  • bin (String)

    The String to encode.

Returns:

  • (String)

    The URL-safe encoded base64 String.


84
85
86
# File 'lib/ronin/formatting/extensions/binary/base64.rb', line 84

def urlsafe_encode64(bin)
  strict_encode64(bin).tr("+/", "-_")
end