Time and IDs

CUID2

CUID2 ids: secure, collision-resistant, starting with a letter, of the length you choose, that give away neither the time nor a count; and a check that a string is one.

Version 1.0.0 · 4 functions · 1 crate

#ids#urls#database#security

Functions

What use cuid2.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use cuid2_new, cuid2_sized from cuid2 (Quillo 0.6).

cuid2_new() → str

A new CUID2 of 24 characters: a lower-case letter, then letters and digits ("tz4a98xxat96iws9zmbrgj3a").

cuid2_sized(length: int) → stror an error

A new CUID2 of length characters, from 2 to 32: short ones for invite codes, long ones where billions are made.

cuid2_many(count: int) → listor an error

count new CUID2s of the default length (at most 10,000), all different: for seeding tables or handing out codes in bulk.

cuid2_valid(id: str) → bool

Whether id has a CUID2's shape: 2 to 32 characters, a lower-case letter first, then lower-case letters and digits. For checking ids in URLs before a database lookup.

Get it

$ quillo install cuid2               # cuid2.qwer into this folder$ quillo install cuid2 --example     # and its example, cuid2.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/cuid2.qwer$ curl -O https://quillolang.org/plugins/files/cuid2.qwe

Then, in your program, all of it or only what you call:

main.qwe
use cuid2.qweruse cuid2_new, cuid2_sized from cuid2      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
cuid2 = "=0.1.6"

Example

cuid2.qwe
// Ids for a shop's orders and invite codes that give nothing away: not the count, not the time.////   quillo run cuid2.qwe      then open http://localhost:8080/idsuse "cuid2.qwer"route GET /ids {    let order = cuid2_new()    let invite = cuid2_sized(10)?    let batch = cuid2_many(1000)?    {        order: order,        order_length: len(order),        invite_length: len(invite),        all_valid: all(batch, (id) => cuid2_valid(id)),        all_different: len(unique(batch)) == 1000,        too_long: cuid2_sized(40).error,    }}// /orders/<id>: an id that is not a CUID2 is refused before any lookup.route GET /orders/:id {    if !cuid2_valid(id) { reply 404 { error: "no such order", checked: id } }    { order: id, valid: true }}
$ quillo run cuid2.qwe$ curl 'localhost:8080/ids'$ curl 'localhost:8080/orders/tz4a98xxat96iws9zmbrgj3a'

Source

cuid2.qwer
//! CUID2 ids: secure, collision-resistant ids that start with a letter and hide when and where they were made, for URLs, database keys and anything a visitor can see.//! version: 1.0.0//!//! [dependencies]//! cuid2 = "=0.1.6"/// A new CUID2 of 24 characters: a lower-case letter, then letters and digits (`"tz4a98xxat96iws9zmbrgj3a"`).pub fn cuid2_new() -> String {    cuid2::create_id()}/// A new CUID2 of `length` characters, from 2 to 32: short ones for invite codes, long ones where billions are made.pub fn cuid2_sized(length: i64) -> Result<String, String> {    if !(2..=32).contains(&length) {        return Err(format!("a CUID2 is 2 to 32 characters long, not {}", length));    }    Ok(cuid2::CuidConstructor::new().with_length(length as u16).create_id())}/// `count` new CUID2s of the default length (at most 10,000), all different: for seeding tables or handing out codes in bulk.pub fn cuid2_many(count: i64) -> Result<Vec<String>, String> {    if !(0..=10_000).contains(&count) {        return Err(format!("from 0 to 10000 ids at a time, not {}", count));    }    Ok((0..count).map(|_| cuid2::create_id()).collect())}/// Whether `id` has a CUID2's shape: 2 to 32 characters, a lower-case letter first, then lower-case letters and digits. For checking ids in URLs before a database lookup.pub fn cuid2_valid(id: &str) -> bool {    cuid2::is_cuid2(id)}
Thread
77Useful? Like it, or add to its thread below.

Thread 0 posts

On CUID2: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.