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
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() → strA new CUID2 of 24 characters: a lower-case letter, then letters and digits ("tz4a98xxat96iws9zmbrgj3a").
cuid2_sized(length: int) → stror an errorA 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 errorcount 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) → boolWhether 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.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/cuid2.qwer$ curl -O https://quillolang.org/plugins/files/cuid2.qweThen, in your program, all of it or only what you call:
use cuid2.qweruse cuid2_new, cuid2_sized from cuid2 // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
cuid2 = "=0.1.6"Example
// 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 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)}Related
nanoid.qwer · 1.0.0Short random ids for links, invites and coupon codes: Nano ID with secure random, your own alphabet and length, readable codes for print, and the odds of a collision.7 views · 7 downloadsShort ids short_ids.qwer · 1.0.0Short ids for URLs made from your database's numbers and read back (Sqids): your own alphabet, a minimum length, no rude words, and typos never reaching another row.6 views · 8 downloadsTypeID typeid.qwer · 1.0.0TypeIDs such as user_01h455vb4pex5vsknk084sn02q: a type prefix and a UUIDv7 in base32, made, read back to their UUID and time, and checked against the type a URL expects.6 views · 7 downloadsKSUID ksuid.qwer · 1.0.0KSUIDs, Segment's K-Sortable Unique IDs: 27 base62 characters sorted by the second they were made, with 128 random bits; made, read back and checked.8 views · 7 downloadsReadable ids human_ids.qwer · 1.0.0Readable ids such as brave-blue-fox for projects, preview URLs and invite codes: random, alliterating, or always the same name for a given number.7 views · 7 downloadsSnowflake ids snowflake.qwer · 1.0.0Snowflake ids: 64-bit numbers that sort by time, unique across 1,024 servers with no coordination, with their time, server and sequence read back and the first id of a time for range queries.7 views · 7 downloadsThread 0 posts
On CUID2: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.