Module: Ob64

Extended by:
LibBase64
Includes:
LibBase64
Defined in:
lib/ob64.rb,
lib/ob64/version.rb,
lib/ob64/core_ext.rb,
ext/ob64/ob64_ext.c

Overview

Methods for base64-encoding and -decoding strings.

Defined Under Namespace

Modules: LibBase64

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.decode(string) ⇒ String

Returns the Base64-decoded version of string. This method complies with RFC 4648. ArgumentError is raised if string is incorrectly padded or contains non-alphabet characters. Note that CR or LF are also rejected.

Parameters:

  • string (String)

Returns:

  • (String)

Raises:

  • (ArgumentError)

    if string cannot be decoded



34
35
36
# File 'lib/ob64.rb', line 34

def decode(string)
  __decode(string)
end

.decoded_length_of(string) ⇒ Integer

Returns the length of the Base64-decoded version of string.

ArgumentError is raised if string has an invalid length.

Parameters:

  • string (String)

Returns:

  • (Integer)

Raises:

  • (ArgumentError)

    if string has an invalid length



93
94
95
# File 'lib/ob64.rb', line 93

def decoded_length_of(string)
  __decoded_length_of(string)
end

.encode(bin) ⇒ String

Returns the Base64-encoded version of bin. This method complies with RFC 4648. No line feeds are added.

Parameters:

  • bin (String)

Returns:

  • (String)


22
23
24
# File 'lib/ob64.rb', line 22

def encode(bin)
  __encode(bin)
end

.encoded_length_of(bin, padding: true) ⇒ Integer

Returns the length of the Base64-encoded version of bin.

Parameters:

  • bin (String)
  • padding (Boolean) (defaults to: true)
    • if the Base64-encoded version of bin will be padded

Returns:

  • (Integer)


82
83
84
# File 'lib/ob64.rb', line 82

def encoded_length_of(bin, padding: true)
  __encoded_length_of(bin, padding)
end

.urlsafe_decode(string) ⇒ String

Returns the Base64-decoded version of string. This method complies with “Base 64 Encoding with URL and Filename Safe Alphabet” in RFC 4648. The alphabet uses ‘-’ instead of ‘+’ and ‘_’ instead of ‘/’.

The padding character is optional. This method accepts both correctly-padded and unpadded input. Note that it still rejects incorrectly-padded input.

Parameters:

  • string (String)

Returns:

  • (String)

Raises:

  • (ArgumentError)

    if string cannot be decoded



67
68
69
70
71
72
73
74
75
# File 'lib/ob64.rb', line 67

def urlsafe_decode(string)
  if !string.end_with?("=") && string.length % 4 != 0
    string = string.ljust((string.length + 3) & ~3, "=")
    string.tr!("-_", "+/")
  else
    string = string.tr("-_", "+/")
  end
  __decode(string)
end

.urlsafe_encode(bin, padding: true) ⇒ String

Returns the Base64-encoded version of bin. This method complies with “Base 64 Encoding with URL and Filename Safe Alphabet” in RFC 4648. The alphabet uses ‘-’ instead of ‘+’ and ‘_’ instead of ‘/’. Note that the result can still contain ‘=’. You can remove the padding by setting padding as false.

Parameters:

  • bin (String)
  • padding (Boolean) (defaults to: true)
    • if the output must be padded

Returns:

  • (String)


48
49
50
51
52
53
# File 'lib/ob64.rb', line 48

def urlsafe_encode(bin, padding: true)
  string = __encode(bin)
  string.chomp!("==") || string.chomp!("=") unless padding
  string.tr!("+/", "-_")
  string
end