2025-01-06 01:12:25 -05:00
|
|
|
Contents
|
|
|
|
========
|
|
|
|
- [Introduction](#introduction)
|
|
|
|
- [Usage](#usage)
|
|
|
|
|
|
|
|
# Introduction
|
|
|
|
This is a Base64 encoding library as described in [RFC4648](https://www.rfc-editor.org/rfc/rfc4648).
|
|
|
|
This library encodes and decodes random data into ASCII characters using an appropriate
|
|
|
|
Base64 library. It is designed to leverage the abstractions in Rust's std library
|
|
|
|
in order to be generic over io streams. That is to say, you can read from anything
|
|
|
|
implementing **Read** and write the encoded or decoded output to anything which
|
|
|
|
implements **Write**.
|
|
|
|
|
|
|
|
# Usage
|
|
|
|
Add the crate to your `Cargo.toml` file.
|
|
|
|
```Toml
|
|
|
|
# Cargo.toml
|
|
|
|
[dependencies]
|
|
|
|
b64 = { git = "https://git.hitchhiker-linux.org/jeang3nie/b64_rs.git" }
|
|
|
|
```
|
|
|
|
## Encoding
|
|
|
|
Encoding 'Hello, World!' into a new `String`, wrapping at 76 characters and using
|
|
|
|
the default alphabet.
|
|
|
|
```
|
|
|
|
use {
|
|
|
|
std::io::Read,
|
|
|
|
b64::Encoder,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut encoder = Encoder::new("Hello, World!".as_bytes(), String::new(), None, Some(76));
|
|
|
|
encoder.encode().unwrap();
|
|
|
|
assert_eq!(encoder.output(), "SGVsbG8sIFdvcmxkIQ==");
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Decoding
|
|
|
|
```
|
|
|
|
use {
|
|
|
|
std::io::Read,
|
|
|
|
b64::Decoder,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
2025-01-08 15:10:39 -05:00
|
|
|
let mut decoder = Decoder::new("SGVsbG8sIFdvcmxkIQ==".as_bytes(), vec![], None, false);
|
2025-01-06 01:12:25 -05:00
|
|
|
let output = decoder.decode().unwrap();
|
|
|
|
assert_eq!(&output, b"Hello, World!");
|
|
|
|
}
|
|
|
|
```
|