#base64-url #base64 #url-safe #encoded-string #character-encoding #16-bit

no-std base64id

Efficiently represent 64, 32 and 16 bit integers as base64url strings

6 releases (3 breaking)

0.4.1 Nov 9, 2024
0.4.0 Nov 9, 2024
0.3.1 Apr 5, 2023
0.3.0 Jan 22, 2023
0.1.1 Oct 4, 2022

#859 in Encoding

Download history 2/week @ 2024-08-19 15/week @ 2024-08-26 2/week @ 2024-09-02 7/week @ 2024-09-09 27/week @ 2024-09-23 28/week @ 2024-09-30 45/week @ 2024-10-14 59/week @ 2024-10-21 70/week @ 2024-10-28 223/week @ 2024-11-04 59/week @ 2024-11-11 23/week @ 2024-11-18 37/week @ 2024-11-25 14/week @ 2024-12-02

191 downloads per month

MIT/Apache

41KB
683 lines

base64id-rs

crates.io official website docs.rs Rust Validation license

A pure rust library for representing 64, 32 and 16 bit integers as base64url encoded strings.

base64url    i64                   u64
-----------  --------------------  --------------------
B21CkMCtWZA    535157120202267024    535157120202267024
fHH_W21Typg   8967229101212682904   8967229101212682904
kjsG-f3NhxI  -7909720649771415790  10537023423938135826
jHamKFSl5oM  -8325284168998721917  10121459904710829699

An integer is efficiently stored and manipulated in memory. However the integer cannot be sent to/from a web client in a url safe manor, without some encoding scheme. This library allows you to encode the integer to/from an base64url character string.

For a video of the underlying concept in action, see here.

Benefits

  • Integers are made url safe
  • Encoded integers use fewer bytes as compared to hex or decimal encoding
  • Tests for RFC 4648 compliance where implemented from the start and across the entire libary
  • base64id uses #![no_std] with no heap allocation required
  • base64id uses #![forbid(unsafe_code)]

Website

You can use base64id.cksm.cc to test, debug and generate random base64id strings instantly.

All conversions are run locally in the browser using JavaScript and Web Assembly, with no server backend needed. You can view the GitHub repo for this website here.

Motivation

I've used this concept a number of times in personal and work projects as I find it very useful. The problem is I've had to reimplement the functionality everytime.

The motivation for this library was to design and implement the core concept once, while paying attention to metrics such as performance, correctness and compatability.

Installation

Add the following to your Cargo.toml file

[dependencies]
base64id = "0.4"

Migrating From v0.3

For users of v0.3.x or less migrating to v0.4.0 or greater, please see the migration guide.

Usage

You start by creating your own tuple struct with a single i64, i32 or i16. Then apply the Base64Id derive macro to your struct.

use base64id::Base64Id;

#[derive(Base64Id)]
struct MyId(i64);

Encoding

You can convert signed or unsigned integers to a Base64Id struct as follows:

use base64id::Base64Id;

#[derive(Base64Id)]
struct MyId(i64);

fn main() {
    let int: i64 = 1;
    let id = MyId::from(int);

    println!("{id}"); // AAAAAAAAAAE
}

Decoding

You can use FromStr and From<{integer}> to convert a String to a Base64Id struct and then into an i64 as follows:

use base64id::{Base64Id, Error};
use std::str::FromStr;

#[derive(Base64Id)]
struct MyId(i64);

fn main() -> Result<(), Error> {
    let id_str = MyId::from_str("PDFehCFVGqA")?;
    let id_int = i64::from(id_str);

    println!("{}", id_int); // 4337351837722417824

    Ok(())
}

Serde

Support for Serde is possible through the use of the base64id derive macro helper attribute.

use base64id::Base64Id;
use serde_json::Result;

#[derive(Base64Id)]
#[base64id(Serialize, Deserialize)]
struct MyId(i32);

fn main() -> Result<()> {
    let id = MyId(897100256);

    println!("{}", serde_json::to_string(&id)?); // "NXip4A"

    Ok(())
}

This will apply Base64Id specific implementations of Serialize and Deserialize to your struct.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~195–630KB
~15K SLoC