Crates.io | englishid |
lib.rs | englishid |
version | 0.3.1 |
source | src |
created_at | 2021-10-24 23:05:57.996506 |
updated_at | 2022-09-07 20:25:11.527972 |
description | English formatting for unsigned integer IDs |
homepage | https://github.com/khonsulabs/englishid |
repository | https://github.com/khonsulabs/englishid |
max_upload_size | |
id | 470594 |
size | 175,217 |
English formatting for unsigned integer IDs and arbitrary data.
English formatting for unsigned integers. Useful for encoding large IDs in a human-readable and recognizable format. Uses a modified list of words based on a list created by the EFF.
Generating an ID can be done from any primitive unsigned integer type:
use englishid::EnglishId;
let english_id = EnglishId::from(42_u16).to_string().unwrap();
assert_eq!(english_id, "accept-abacus");
Use the corresponding parse method to extract the encoded id:
let parsed = englishid::parse_u16("accept-abacus").unwrap();
assert_eq!(parsed, 42);
The wordlist used can encode 52 bits of information in 4 words. If you'd prefer to restrict your u64 IDs to 52 bits, you can set the number of words used:
use englishid::EnglishId;
let english_id = EnglishId::from(123456789_u64).words(4).to_string().unwrap();
assert_eq!(english_id, "haunt-subtitle-abandon-abacus");
assert_eq!(englishid::parse_u64(&english_id).unwrap(), 123456789_u64);
If a value is ever out of acceptable ranges, Error::ValueOutOfRange
will be
returned.
This crate also offers functions that allow encoding arbitrary bytes of
information using the same word list. If you will always know the data size, you
can use the fixed_length
functions:
let payload = b"hello world";
let encoded = englishid::encode_fixed_length(payload);
assert_eq!(encoded, "hatchback-reissue-residual-overbuilt-ladybug-tusk-buffing");
assert_eq!(englishid::decode_fixed_length(&encoded, payload.len()).unwrap(), payload);
If you are encoding payloads of differing lengths and want the length to be
encoded into the resulting englishid
string, encode()
and decode()
will do
that for you:
let payload = b"hello world";
let encoded = englishid::encode(payload).unwrap();
assert_eq!(encoded, "able-hatchback-reissue-residual-overbuilt-ladybug-tusk-buffing");
assert_eq!(englishid::decode(&encoded).unwrap(), payload);
Or, if you have an enum that can correspond to a byte length, you can use a custom header value:
enum PrivateKey {
Ed25519([u8; 32]),
Ed448([u8; 56])
}
impl PrivateKey {
fn as_bytes(&self) -> &[u8] {
match self {
Self::Ed25519(key) => key,
Self::Ed448(key) => key,
}
}
fn kind(&self) -> u16 {
match self {
Self::Ed25519(_) => 1,
Self::Ed448(_) => 2,
}
}
fn byte_length(kind: u16) -> usize {
match kind {
1 => 32,
2 => 56,
_ => 0,
}
}
}
let key = PrivateKey::Ed25519([0; 32]);
let encoded = englishid::encode_with_custom_header(key.as_bytes(), key.kind()).unwrap();
assert_eq!(englishid::decode_with_custom_header(&encoded, PrivateKey::byte_length).unwrap(), key.as_bytes());
When encoding using the fixed_length
APIs, there is no limit to the amount of
data that can be encoded.
When using the automatic length header or a custom header, the value in the header cannot be larger than 8190. This limit may be removed in the future, but this crate is not intended for large payload encoding.
The maintainers of this crate will treat changes to the wordlist as breaking changes in the eyes of semantic versioning. In the future, this crate may support additional wordlists which will provide another mechanism to releasing wordlist updates, which should be infrequent.
If an issue is discovered that generated data that was unable to be recovered into its original form, fixes will be shipped on minor releases and versions that can generate invalid data will be yanked.
This project, like all projects from Khonsu Labs, are open-source. This repository is available under the MIT License or the Apache License 2.0.
To learn more about contributing, please see CONTRIBUTING.md.