Baid64 (Base64 Aided Identities) is an enhanced Base64 encoding scheme designed for secure, human-verifiable identity encoding. It extends standard Base64 with Human Readable Identifiers (HRI), cryptographic check sums, mnemonic representations, and optional formatting features to create a robust encoding system suitable for identity management and secure data representation.
Baid64 uses a modified Base64 alphabet optimized for URL-safety and human readability:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~
Key differences from standard Base64:
- Uses
_instead of+ - Uses
~instead of/ - No padding characters (
=)
A complete Baid64 encoded string consists of the following components:
[HRI:]<base64-data>[#mnemonic]
Where:
- HRI (optional): Human Readable Identifier prefix (max 16 characters)
- base64-data: The encoded payload, optionally with embedded checksum
- mnemonic (optional): Human-readable checksum representation
The checksum is computed using a double SHA-256 hash:
fn check(hri: &str, payload: [u8; LEN]) -> [u8; 4] {
let key = SHA256(hri.as_bytes())
let mut sha = SHA256_with_prefix(key)
sha.update(payload)
let result = sha.finalize()
return [result[0], result[1], result[1], result[2]] // Note: result[1] is duplicated
}Important: The checksum intentionally duplicates result[1] in positions 1 and 2 of the output array.
The payload is encoded using the custom Baid64 alphabet:
- If
EMBED_CHECKSUMis true, append the 4-byte checksum to the payload - Encode the combined data using the Baid64 alphabet
- No padding is added
When chunking is enabled, the encoded string is formatted as:
- First chunk: 8 characters (configurable via
CHUNK_FIRST) - Subsequent chunks: 7 characters each (configurable via
CHUNK_LEN) - Chunks are separated by hyphens (
-)
Example: ABCDEFGH-ijklmno-pqrstuv-wxyz012
The final format follows this pattern:
- Prefix (if enabled): Add
HRI:before the encoded data - Encoded Data: The base64-encoded payload (possibly chunked)
- Suffix (if enabled): Add
#mnemonicafter the encoded data
- Extract HRI if present (text before
:) - Extract mnemonic if present (text after
#) - Remove any chunking hyphens from the base64 data
Decode the cleaned base64 string using the Baid64 alphabet to retrieve:
- The original payload (first LEN bytes)
- Optional embedded checksum (last 4 bytes if present)
- Calculate the expected checksum using the extracted HRI and payload
- Compare with:
- The embedded checksum (if present in the data)
- The mnemonic-derived checksum (if mnemonic suffix present)
- Fail if checksums don't match
Return the validated payload bytes.
The DisplayBaid64 trait provides several configuration constants:
| Constant | Type | Description |
|---|---|---|
HRI |
&'static str |
Human Readable Identifier string |
CHUNKING |
bool |
Enable/disable chunking of output |
PREFIX |
bool |
Include HRI prefix in output |
EMBED_CHECKSUM |
bool |
Embed checksum in base64 data |
MNEMONIC |
bool |
Include mnemonic suffix |
CHUNK_FIRST |
usize |
Length of first chunk (default: 8) |
CHUNK_LEN |
usize |
Length of subsequent chunks (default: 7) |
- Uses SHA-256 based checksum with HRI as salt
- Provides 32 bits of error detection
- Checksum can be verified via embedded data or mnemonic
- Mnemonic representation allows verbal checksum confirmation
- Chunking improves readability and reduces transcription errors
- HRI provides context and prevents cross-domain confusion
- Minimum payload length: 4 bytes (
ID_MIN_LEN) - Maximum HRI length: 16 bytes (
HRI_MAX_LEN) - Fixed checksum size: 4 bytes
The system defines several error types:
| Error Type | Description |
|---|---|
InvalidHri |
HRI doesn't match expected value |
InvalidLen |
Payload length doesn't match expected size |
InvalidChecksum |
Checksum verification failed |
InvalidMnemonicLen |
Mnemonic doesn't decode to 4 bytes |
InvalidMnemonic |
Mnemonic decode error |
Base64 |
Base64 decode error |
InvalidPayload |
Payload validation failed |
let data = TestBaid64 { payload: [1; 32] };
let encoded = data.to_baid64_string();
// Result: "testHRI:AQEBAQEB...#word1-word2-word3"let decoded = TestBaid64::from_baid64_str("testHRI:AQEBAQEB...#word1-word2-word3")?;Using Rust's format specifiers to control output:
format!("{}", display) // Default: with prefix and suffix
format!("{:-}", display) // Suppress prefix
format!("{:#}", display) // Suppress suffix-
Checksum Duplication: The checksum algorithm intentionally duplicates
sha[1]to create a 4-byte checksum from 3 unique bytes. -
Mnemonic Integration: The mnemonic encoding is handled by an external
mnemoniccrate, converting the 4-byte checksum into memorable words. -
Flexibility: The trait-based design allows different types to customize encoding parameters while maintaining consistent behavior.
-
Round-trip Safety: The encoding and decoding processes are designed to be perfectly reversible when all components are preserved.
base64: Core Base64 encoding/decodingsha2: SHA-256 hashing for checksumsmnemonic: Checksum to mnemonic word conversionamplify: Utility macros and derive support