Go to file
2025-01-09 23:24:59 -05:00
src Overall code cleanup 2025-01-09 23:24:59 -05:00
.gitignore Initial commit 2024-05-01 16:08:33 -04:00
Cargo.lock Initial commit 2024-05-01 16:08:33 -04:00
Cargo.toml Initial commit 2024-05-01 16:08:33 -04:00
README.md Change from fmt::Write to io::Write for Encoder 2025-01-09 19:09:15 -05:00

Contents

Introduction

This is a Base64 encoding library as described in 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.

# 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(), vec![], None, Some(76));
    encoder.encode().unwrap();
    assert_eq!(encoder.output(), b"SGVsbG8sIFdvcmxkIQ==");
}

Decoding

use {
    std::io::Read,
    b64::Decoder,
};

fn main() {
    let mut decoder = Decoder::new("SGVsbG8sIFdvcmxkIQ==".as_bytes(), vec![], None, false);
    let output = decoder.decode().unwrap();
    assert_eq!(&output, b"Hello, World!");
}