#base32 #identifier #codec #numeric #u64 #values

crockford

Provides Crockford Base32 encoding for identifiers (e.g. u64 values).

11 releases (7 stable)

1.2.1 Nov 18, 2023
1.1.2 May 8, 2023
1.1.0 Feb 1, 2021
1.0.1 Jan 6, 2018
0.1.0 May 24, 2017

#255 in Encoding

Download history 157/week @ 2024-07-01 117/week @ 2024-07-08 175/week @ 2024-07-15 475/week @ 2024-07-22 593/week @ 2024-07-29 498/week @ 2024-08-05 460/week @ 2024-08-12 238/week @ 2024-08-19 85/week @ 2024-08-26 111/week @ 2024-09-02 74/week @ 2024-09-09 34/week @ 2024-09-16 92/week @ 2024-09-23 129/week @ 2024-09-30 76/week @ 2024-10-07 112/week @ 2024-10-14

409 downloads per month
Used in 3 crates

MIT/Apache

17KB
266 lines

crockford

Base32 encoding for 64-bit values.

Crockford Base32 Encoding is most commonly used to make numeric identifiers slightly more user-resistant. Similar to Hashids, the purpose here is to make the identifiers shorter and less confusing. Unlike Hashids, Crockford Base32 does nothing to conceal the real value of the number (beyond the actual encoding, anyway) and the fact that they are sequential is still pretty obvious when you see consecutive identifiers side by side.

This library does not support encoding and decoding of arbitrary data; there is another library for that. Additionally, the spec supports the idea of check digits, but this library currently does not.

The primary purpose of this library is to provide high performance, user-resistant encoding of numeric identifiers. To that end, both encoding and decoding are, in fact, pretty darn fast. How fast? According to my testing, crockford decodes fifty times faster and encodes twenty-seven times faster than harsh.

Usage

Encoding

Encoding is a one-step process.

let x = crockford::encode(5111);
assert_eq!("4ZQ", &*x);

If you want lowercase, then... Well, tough. However, we do now support encoding to a buffer of your choice rather than a new one created in the function. Read on to learn about plan B...

Plan B (faster encoding)

Because this is Rust, particular focus is given to runtime efficiency--or, at least, allowing the user to achieve runtime efficiency. As a result, we provide a second, more complicated encoding option.

// The longest possible representation of u64 is 13 digits.
let mut buf = Vec::with_capacity(13);
crockford::encode_into(5111, &mut buf);

let result = std::str::from_utf8(&buf)?;
assert_eq!("4ZQ", result);

This encode_into method also accepts &mut String, if you prefer.

Decoding

Decoding is a two-step process. This is because you can feed any string to the decoder, and the decoder will return an error if you try to convince it that "Hello, world!" is a number. (Hint: it isn't.)

let x = crockford::decode("4zq");
let y = crockford::decode("4ZQ");

assert_eq!(5111, x?);
assert_eq!(5111, y?);

So, step one is to call the decode function. Step two is to match/verify/unwrap/throw away the output.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps