module Base64
Overview
The Base64 module provides for the encoding (#encode
, #strict_encode
,
#urlsafe_encode
) and decoding (#decode
, strict_decode
, urlsafe_decode
)
of binary data using a Base64 representation.
Example
A simple encoding and decoding.
require "base64"
enc = Base64.encode("Send reinforcements")
# => "U2VuZCByZWluZm9yY2VtZW50cw==\n"
plain = Base64.decode(enc)
# => "Send reinforcements"
The purpose of using base64 to encode data is that it translates any binary data into purely printable characters.
Extended Modules
Defined in:
base64.crInstance Method Summary
-
#decode(data, io : IO)
Write the Base64-decoded version of
data
toio
. -
#decode(data) : Bytes
Returns the Base64-decoded version of
data
as a Slice(UInt8). -
#decode_string(data) : String
Returns the Base64-decoded version of
data
as a string. -
#encode(data, io : IO)
Write the Base64-encoded version of
data
toio
. -
#encode(data) : String
Returns the Base64-encoded version of
data
. -
#strict_encode(data, io : IO)
Write the Base64-encoded version of
data
with no newlines toio
. -
#strict_encode(data) : String
Returns the Base64-encoded version of
data
with no newlines. -
#urlsafe_encode(data, io : IO)
Write the Base64-encoded version of
data
using a urlsafe alphabet toio
. -
#urlsafe_encode(data, padding = false) : String
Returns the Base64-encoded version of
data
using a urlsafe alphabet.
Instance Method Detail
Write the Base64-decoded version of data
to io
.
This will decode either the normal or urlsafe alphabets.
Returns the Base64-decoded version of data
as a Slice(UInt8).
This will decode either the normal or urlsafe alphabets.
Returns the Base64-decoded version of data
as a string.
If the data doesn't decode to a valid UTF8 string,
InvalidByteSequenceError will be raised.
This will decode either the normal or urlsafe alphabets.
Write the Base64-encoded version of data
to io
.
This method complies with RFC 2045.
Line feeds are added to every 60 encoded characters.
require "base64"
Base64.encode("Now is the time for all good coders\nto learn Crystal", io)
Returns the Base64-encoded version of data
.
This method complies with RFC 2045.
Line feeds are added to every 60 encoded characters.
require "base64"
puts Base64.encode("Now is the time for all good coders\nto learn Crystal")
Generates:
Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g
Write the Base64-encoded version of data
with no newlines to io
.
This method complies with RFC 4648.
require "base64"
Base64.strict_encode("Now is the time for all good coders\nto learn Crystal", io)
Returns the Base64-encoded version of data
with no newlines.
This method complies with RFC 4648.
require "base64"
puts Base64.strict_encode("Now is the time for all good coders\nto learn Crystal")
Generates:
Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4gQ3J5c3RhbA==
Write the Base64-encoded version of data
using a urlsafe alphabet to io
.
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
parameter defaults to false. When true, enough =
characters
are added to make the output divisible by 3.
Returns the Base64-encoded version of data
using a urlsafe alphabet.
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
parameter defaults to false. When true, enough =
characters
are added to make the output divisible by 3.