Module std.base64
Support for Base64 encoding and decoding.
This module provides two default implementations of Base64 encoding,
Base64 with a standard encoding alphabet, and a variant
Base64URL that has a modified encoding alphabet designed to be
safe for embedding in URLs and filenames.
Both variants are implemented as instantiations of the template
Base64Impl. Most users will not need to use this template
directly; however, it can be used to create customized Base64 encodings,
such as one that omits padding characters, or one that is safe to embed
inside a regular expression.
Example
ubyte[] data = [0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e];
const(char)[] encoded = Base64 .encode(data);
assert(encoded == "FPucA9l+");
ubyte[] decoded = Base64 .decode("FPucA9l+");
assert(decoded == [0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e]);
The range API is supported for both encoding and decoding:
Example
// Create MIME Base64 with CRLF, per line 76.
File f = File("./text.txt", "r");
scope(exit) f .close();
Appender!string mime64 = appender!string;
foreach (encoded; Base64 .encoder(f .byChunk(57)))
{
mime64 .put(encoded);
mime64 .put("\r\n");
}
writeln(mime64 .data);
References
Classes
| Name | Description |
|---|---|
Base64Exception
|
Exception thrown upon encountering Base64 encoding or decoding errors. |
Templates
| Name | Description |
|---|---|
Base64Impl
|
Template for implementing Base64 encoding and decoding. |
Aliases
| Name | Type | Description |
|---|---|---|
Base64
|
Base64Impl!('+','/')
|
Implementation of standard Base64 encoding. |
Base64URL
|
Base64Impl!('-','_')
|
Variation of Base64 encoding that is safe for use in URLs and filenames. |
Base64URLNoPadding
|
Base64Impl!('-','_',Base64.NoPadding)
|
Unpadded variation of Base64 encoding that is safe for use in URLs and filenames, as used in RFCs 4648 and 7515 (JWS/JWT/JWE). |
Authors
Masahiro Nakagawa, Daniel Murphy (Single value Encoder and Decoder)