A base64 string encoder and decoder library written in Rust with zero dependencies.
use b64::encode_data;
fn main() {
let input: String = String::from("Hello World!");
let enc: String = encode_data(input.as_bytes());
println!("Encoded Data: {}", enc);
}
use b64::decode_data;
fn main() {
let input: String = String::from("SGVsbG8gV29ybGQh");
let dec: Vec<u8> = decode_data(input);
let string: String = String::from_utf8(dec).expect("The computed bytes are not UTF-8!");
println!("Decoded Data: {}", string);
}
use b64::is_valid_b64;
fn main() {
let input1: String = String::from("ABCDEFGH");
let input2: String = String::from("ABSJF$#A");
println!("Is '{}' a valid base64 string: {}", input1, is_valid_b64(&input1));
println!("Is '{}' a valid base64 string: {}", input2, is_valid_b64(&input2));
}
The library provides two public functions, one each for encoding and decoding. Their signatures are:
fn encode_data(data: &[u8]) -> String
fn decode_quadruplet(data: &str) -> Vec<u8>
fn is_valid_b64(data: &String) -> bool
- Clone this repo
- Build using
cargo build
Some basic tests are provided in the repo, those can be run using
cargo test
The library has documentation comments. Build the docs using
cargo doc