{"id":19562674,"url":"https://github.com/jamsocket/block-id","last_synced_at":"2025-12-12T13:59:21.624Z","repository":{"id":57307154,"uuid":"475426524","full_name":"jamsocket/block-id","owner":"jamsocket","description":null,"archived":false,"fork":false,"pushed_at":"2023-04-27T15:51:43.000Z","size":41,"stargazers_count":29,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-24T11:46:47.440Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamsocket.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-29T12:12:02.000Z","updated_at":"2025-04-20T03:36:08.000Z","dependencies_parsed_at":"2024-05-16T06:32:06.147Z","dependency_job_id":"aa542775-a587-405f-8d42-05e9c834879c","html_url":"https://github.com/jamsocket/block-id","commit_stats":null,"previous_names":["jamsocket/block-id"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamsocket%2Fblock-id","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamsocket%2Fblock-id/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamsocket%2Fblock-id/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamsocket%2Fblock-id/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamsocket","download_url":"https://codeload.github.com/jamsocket/block-id/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251072279,"owners_count":21532004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T05:15:20.379Z","updated_at":"2025-12-12T13:59:21.573Z","avatar_url":"https://github.com/jamsocket.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `block-id`\n\n[![GitHub Repo stars](https://img.shields.io/github/stars/drifting-in-space/block-id?style=social)](https://github.com/drifting-in-space/block-id)\n[![wokflow state](https://github.com/drifting-in-space/block-id/workflows/Rust/badge.svg)](https://github.com/drifting-in-space/block-id/actions/workflows/rust.yml)\n[![crates.io](https://img.shields.io/crates/v/block-id.svg)](https://crates.io/crates/block-id)\n[![docs.rs](https://img.shields.io/badge/docs-release-brightgreen)](https://docs.rs/block-id/)\n[![dependency status](https://deps.rs/repo/github/drifting-in-space/block-id/status.svg)](https://deps.rs/repo/github/drifting-in-space/block-id)\n\n`block-id` is a Rust library for generating opaque, unique, and short string values from (unsigned) integers.\n\n**tl;dr:**\n\n```rust\nuse block_id::{Alphabet, BlockId};\n\nfn main() {\n    // Random seed.\n    let seed = 9876;\n    \n    // Code length.\n    let length = 5;\n\n    let generator = BlockId::new(Alphabet::alphanumeric(), seed, length);\n    \n    // Number to string.\n    assert_eq!(Some(\"wjweA\".to_string()), generator.encode_string(0));\n    assert_eq!(Some(\"ZxJrE\".to_string()), generator.encode_string(1));\n    assert_eq!(Some(\"3e0IT\".to_string()), generator.encode_string(2));\n\n    // String to number.\n    assert_eq!(Some(2), generator.decode_string(\"3e0IT\"));\n}\n```\n\n## Introduction\n\nRandom-looking alphanumeric strings are often used in place of sequential numeric IDs for user-facing purposes. This has several advantages:\n\n- String identifiers are usually visually distinct, even if they were generated adjacently in sequence.\n- The higher information density of a larger alphabet allows for shorter codes.\n- Sequential identifiers reveal unnecessary information about ordering and object creation rate that you may not want to reveal.\n\n`block-id` is the successor to [`tiny_id`](https://github.com/drifting-in-space/block-id), which allows the creation of tightly-packed alphanumeric strings. `tiny_id` turned out to be difficult to use in a distributed environment because its state needs to be synchronized across every node that needs to generate IDs. Rather than building distributed functionality into a short ID generator, `block-id` provides a way of turning a sequential ID generator into a string ID generator by creating a one-to-one mapping between integers and random-looking short strings. That way, any system of generating sequential numeric IDs (for example, a database's sequence generator) can be turned into a system for generating random-looking string IDs.\n\n```rust\nuse block_id::{Alphabet, BlockId};\n\nfn main() {\n    // The alphabet determines the set of valid characters in an ID.\n    // For convenience, we include some common alphabets like `alphanumeric`. \n    let alphabet = Alphabet::alphanumeric();\n    \n    // The generator takes a u128 as a seed.\n    let seed = 1234;\n\n    // The length of a generated code. This is really a _minimum_ length; larger numbers\n    // will be converted to longer codes since that's the only way to avoid collisions.\n    let length = 4;\n\n    // A small amount of pre-caching work happens when we create the BlockId instance,\n    // so it's good to re-use the same generator where possible.\n    let generator = BlockId::new(alphabet, seed, length);\n    \n    // Now that we have a generator, we can turn numbers into short IDs.\n    assert_eq!(Some(\"In4R\".to_string()), generator.encode_string(0));\n\n    assert_eq!(Some(\"4A7N\".to_string()), generator.encode_string(440));\n    assert_eq!(Some(\"tSp9\".to_string()), generator.encode_string(441));\n    assert_eq!(Some(\"6z6y\".to_string()), generator.encode_string(442));\n    assert_eq!(Some(\"ft0M\".to_string()), generator.encode_string(443));\n\n    // When we've exhausted all 4-digit codes, we simply move on to 5-digit codes.\n    assert_eq!(Some(\"YeyKs\".to_string()), generator.encode_string(123456789));\n\n    // ...and so on.\n    assert_eq!(Some(\"pFbrRf\".to_string()), generator.encode_string(1234567890));\n\n    // Codes are reversible, assuming we have the seed they were generated with.\n    assert_eq!(Some(1234567890), generator.decode_string(\"pFbrRf\"));\n}\n```\n\n## How it works\n\n`block-id` applies a pipeline of reversible transformations on a data in order to turn it into a string.\n\n- **Base conversion** turns the input integer into a base-N representation where N is the number of characters in the desired output alphabet.\n- Rounds consisting of:\n    - **Permutation** applies an N-to-N map to every digit of the base-N representation. The permutation is generated from the random seed passed in the `BlockId` constructor.\n    - **Cascade** applies a left-to-right cumulative sum, modulo N, to the base-N representation.\n    - **Rotate** takes the first digit of the base-N representation and moves it to the last digit, shifting all of the other digits left by one.\n- **Alphabetization** translates every digit of the base-N representation to a “letter” in the alphabet provided at construction.\n\nThe number of rounds is the same as the number of digits in the base-N representation. This gives every digit a chance to influence every other digit.\n\n## Security\n\n`block-id` is designed to make it easy for a human to distinguish between two sequential codes, *not* to make it impossible for an adversary to reverse. It should not be considered cryptographically secure.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamsocket%2Fblock-id","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamsocket%2Fblock-id","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamsocket%2Fblock-id/lists"}