#random-string #id #unique-id #encode #obfuscate #sql #integer-value

no-std squint

Encode sequential integer ids as random looking strings

4 releases

0.1.4 Aug 20, 2024
0.1.3 Jul 28, 2024
0.1.2 Jul 22, 2024
0.1.1 Jun 26, 2024
0.1.0 Jun 24, 2024

#361 in Cryptography

Download history 274/week @ 2024-06-23 32/week @ 2024-06-30 101/week @ 2024-07-21 134/week @ 2024-07-28 1/week @ 2024-08-04 125/week @ 2024-08-18 7/week @ 2024-08-25 1/week @ 2024-09-01 10/week @ 2024-09-15 2/week @ 2024-09-22 39/week @ 2024-09-29 4/week @ 2024-10-06

55 downloads per month

MIT/Apache

15KB
301 lines

Squint

github crates.io docs.rs build status

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).

Usage

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

Comparison

UUID(v4)

Pros
  • The most adopted standard for public resource IDs
Cons
  • Not sequential, hence slower database inserts

Cuid and NanoID are similar to UUID relative to this crate


ULID

Pros
  • Lexicographically sortable
  • Compatible with UUID
Cons
  • Contain creation timestamps

Sqids

Pros
  • Can encode multiple numbers in one ID
  • Enable use of auto-incrementing database primary keys
Cons
  • Increased code complexity
  • Can be decoded revealing ID count
  • No built-in solution to ID reuse across entities

This crate

Pros
  • Enable use of auto-incrementing database primary keys
  • Cryptographically secure
Cons
  • Increased code complexity

Dependencies

~600KB
~14K SLoC