Crates.io | squint |
lib.rs | squint |
version | 0.1.4 |
source | src |
created_at | 2024-06-24 17:27:38.258222 |
updated_at | 2024-08-20 17:53:00.162641 |
description | Encode sequential integer ids as random looking strings |
homepage | |
repository | https://github.com/grimerssy/squint |
max_upload_size | |
id | 1282393 |
size | 33,977 |
Squint is a library for encoding integers as unique deterministic strings.
It is expected to be used for encoding database IDs as random strings to get fast indexed database lookups and hide actual IDs from the end users.
Library also provides an easy way to introduce different ID types
(i.e. UserId(1)
shouldn't be equal to CrateId(1)
even though the underlying integer value is the same).
Basic example
use squint::aes::{cipher::KeyInit, Aes128};
type Id = squint::Id<0>;
let key = [0; 16];
let cipher = Aes128::new(&key.into());
let id = Id::new(1, &cipher);
let encoded = id.to_string();
assert_eq!("xZV3JT8xVMefhiyrkTsd4T2", &encoded);
let decoded = encoded
.parse::<Id>()
.and_then(|id| id.to_raw(&cipher))
.unwrap();
assert_eq!(decoded, 1);
Different ID types
use squint::{
aes::{cipher::KeyInit, Aes128},
tag, Id,
};
type UserId = Id<{ tag("user") }>;
type CrateId = Id<{ tag("crate") }>;
let key = [0; 16];
let cipher = Aes128::new(&key.into());
let user_id = UserId::new(1, &cipher);
let crate_id = CrateId::new(1, &cipher);
assert_eq!("qXfXkNN9ReZCGXu3qi28xC2", &user_id.to_string());
assert_eq!("VgtE1tzjDEHnjd3fh3PwiT2", &crate_id.to_string());
Cuid and NanoID are similar to UUID relative to this crate