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
-
.decode(string) ⇒ String
Returns the Base64-decoded version of
string. -
.decoded_length_of(string) ⇒ Integer
Returns the length of the Base64-decoded version of
string. -
.encode(bin) ⇒ String
Returns the Base64-encoded version of
bin. -
.encoded_length_of(bin, padding: true) ⇒ Integer
Returns the length of the Base64-encoded version of
bin. -
.urlsafe_decode(string) ⇒ String
Returns the Base64-decoded version of
string. -
.urlsafe_encode(bin, padding: true) ⇒ String
Returns the Base64-encoded version of
bin.
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.
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.
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.
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.
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.
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.
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 |