module Base64
Overview
The Base64
module provides for the encoding (#encode
, #strict_encode
,
#urlsafe_encode
) and decoding (#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_string(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.crConstant Summary
-
CHARS_SAFE =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
-
CHARS_STD =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
-
DECODE_TABLE =
Array(Int8).new(256) do |i| case i.unsafe_chr when 'A'..'Z' (i - 65).to_i8 when 'a'..'z' (i - 71).to_i8 when '0'..'9' (i + 4).to_i8 when '+', '-' 62_i8 when '/', '_' 63_i8 else -1_i8 end end
-
LINE_SIZE =
60
-
NL =
'\n'.ord.to_u8
-
NR =
'\r'.ord.to_u8
-
PAD =
'='.ord.to_u8
Instance Method Summary
-
#decode(data, io : IO)
Writes the base64-decoded version of data to io.
-
#decode(data) : Bytes
Returns the base64-decoded version of data as a
Bytes
. -
#decode_string(data) : String
Returns the base64-decoded version of data as a string.
-
#encode(data, io : IO)
Writes the base64-encoded version of data to io.
-
#encode(data) : String
Returns the base64-encoded version of data.
-
#strict_encode(data, io : IO)
Writes the base64-encoded version of data with no newlines to io.
-
#strict_encode(data) : String
Returns the base64-encoded version of data with no newlines.
-
#urlsafe_encode(data, io : IO)
Writes the base64-encoded version of data using a urlsafe alphabet to io.
-
#urlsafe_encode(data, padding = true) : String
Returns the base64-encoded version of data using a urlsafe alphabet.
Instance Method Detail
Writes 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 Bytes
.
This will decode either the normal or urlsafe alphabets.
Returns the base64-decoded version of data as a string. This will decode either the normal or urlsafe alphabets.
Writes the base64-encoded version of data to io. This method complies with RFC 2045. Line feeds are added to every 60 encoded characters.
Base64.encode("Now is the time for all good coders\nto learn Crystal", STDOUT)
Returns the base64-encoded version of data. This method complies with RFC 2045. Line feeds are added to every 60 encoded characters.
puts Base64.encode("Now is the time for all good coders\nto learn Crystal")
Generates:
Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g
Q3J5c3RhbA==
Writes the base64-encoded version of data with no newlines to io. This method complies with RFC 4648.
Base64.strict_encode("Now is the time for all good coders\nto learn Crystal", STDOUT)
Returns the base64-encoded version of data with no newlines. This method complies with RFC 4648.
puts Base64.strict_encode("Now is the time for all good coders\nto learn Crystal")
Generates:
Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4gQ3J5c3RhbA==
Writes 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 '/'
.
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 true
. When false
, enough =
characters
are not added to make the output divisible by 4.