Skip to content

A zero dependency Base64 encoder/decoder in Rust

Notifications You must be signed in to change notification settings

Nemesis-AS/b64-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

b64-rs

A base64 string encoder and decoder library written in Rust with zero dependencies.

Getting Started

Encoding data

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);
}

Decoding data

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);
}

Check if a string is valid base64 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));
}

API

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

Builing from Source

  1. Clone this repo
  2. Build using
cargo build

Testing

Some basic tests are provided in the repo, those can be run using

cargo test

Docs

The library has documentation comments. Build the docs using

cargo doc

About

A zero dependency Base64 encoder/decoder in Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages