Expand description
Base64 encoding and decoding.
base64
provides encoding and decoding for Base64,
a binary-to-text encoding scheme that represents binary data
in a printable ASCII string format.
Base64 is commonly used for encoding binary data in contexts
where only text can be stored or transmitted,
such as email attachments, URLs, and data URIs.
The crate supports multiple Base64 alphabets and configurations,
including standard Base64, URL-safe Base64, and custom alphabets.
The main functions are encode
and decode
for simple operations,
and the Engine
trait for more advanced usage with custom configurations.
§Examples
Basic encoding and decoding:
use base64::{encode, decode};
let original = "Hello, world!";
let encoded = encode(original);
println!("Encoded: {}", encoded); // "SGVsbG8sIHdvcmxkIQ=="
let decoded = decode(&encoded).unwrap();
let decoded_str = String::from_utf8(decoded).unwrap();
println!("Decoded: {}", decoded_str); // "Hello, world!"
Using URL-safe encoding for use in URLs:
use base64::{Engine as _, engine::general_purpose};
let data = b"data with/special+chars";
let encoded = general_purpose::URL_SAFE_NO_PAD.encode(data);
println!("URL-safe: {}", encoded); // Uses - and _ instead of + and /
let decoded = general_purpose::URL_SAFE_NO_PAD.decode(&encoded).unwrap();
assert_eq!(decoded, data);
Modules§
- alphabet
- Provides Alphabet and constants for alphabets commonly used in the wild.
- display
- Enables base64’d output anywhere you might use a
Display
implementation, like a format string. - engine
- Provides the Engine abstraction and out of the box implementations.
- prelude
- Preconfigured engines for common use cases.
- read
- Implementations of
io::Read
to transparently decode base64. - write
- Implementations of
io::Write
to transparently handle base64.
Enums§
- Decode
Error - Errors that can occur while decoding.
- Decode
Slice Error - Errors that can occur while decoding into a slice.
- Encode
Slice Error - Errors that can occur while encoding into a slice.
Traits§
- Engine
- An
Engine
provides low-level encoding and decoding operations that all other higher-level parts of the API use. Users of the library will generally not need to implement this.
Functions§
- decode
Deprecated - Decode base64 using the
STANDARD
engine. - decode_
engine Deprecated - Decode from string reference as octets using the specified Engine.
- decode_
engine_ slice Deprecated - Decode the input into the provided output slice.
- decode_
engine_ vec Deprecated - Decode from string reference as octets.
- decoded_
len_ estimate - Returns a conservative estimate of the decoded size of
encoded_len
base64 symbols (rounded up to the next group of 3 decoded bytes). - encode
Deprecated - Encode arbitrary octets as base64 using the
STANDARD
engine. - encode_
engine Deprecated - Encode arbitrary octets as base64 using the provided
Engine
into a newString
. - encode_
engine_ slice Deprecated - Encode arbitrary octets as base64 into a supplied slice.
- encode_
engine_ string Deprecated - Encode arbitrary octets as base64 into a supplied
String
. - encoded_
len - Calculate the base64 encoded length for a given input length, optionally including any appropriate padding bytes.