Expand description
Rust implementation of Crockford’s Base32 encoding scheme.
§Implementation
- Lightweight — The core functionality has zero external dependencies.
- Portable — Fully compatible with
#![no_std]
environments. - Safe — Implemented entirely in safe Rust with no
unsafe
blocks.
let bytes = b"usque ad finem";
let encoded = c32::encode(&bytes);
assert_eq!(encoded, "1TQ6WBNCMG62S10CSMPWSBD");
let bytes = b"usque ad finem";
let decoded = c32::decode("1TQ6WBNCMG62S10CSMPWSBD")?;
assert_eq!(decoded, bytes);
§In #![no_std]
Environments
For environments without allocation support, the library provides buffer-based APIs:
// encoding with a pre-allocated buffer
let bytes = b"usque ad finem";
let mut buffer = [0; 32];
let written = c32::encode_into(bytes, &mut buffer)?;
let encoded = &buffer[..written];
assert_eq!(encoded, b"1TQ6WBNCMG62S10CSMPWSBD");
// decoding with a pre-allocated buffer
let encoded = b"1TQ6WBNCMG62S10CSMPWSBD";
let mut buffer = [0; 32];
let written = c32::decode_into(encoded, &mut buffer)?;
let decoded = &buffer[..written];
assert_eq!(decoded, b"usque ad finem");
§Checksums
The check
feature enables methods for encoding data with SHA256-based
checksum verification.
The encoded data follows this layout:
[version (1B)] + [payload (nB)] + [checksum (4B)]
And is computed by…
1. Concatenating the version byte with the payload bytes.
2. Taking the SHA256 hash of the concatenated bytes.
3. Taking the SHA256 hash of the result.
4. Using the first 4 bytes as the checksum.
§Examples
let bytes = b"usque ad finem";
let encoded = c32::encode_check(bytes, 22)?;
assert_eq!(encoded, "P7AWVHENJJ0RB441K6JVK5DNJ7J3V5");
let encoded = "P7AWVHENJJ0RB441K6JVK5DNJ7J3V5";
let (decoded, version) = c32::decode_check(encoded)?;
assert_eq!(decoded, b"usque ad finem");
assert_eq!(version, 22);
§Features
Feature | Description |
---|---|
alloc | Allocation-based API via encode and decode |
check | Support for checksum validation |
For more details, please refer to the full API Reference.
Modules§
- checksum
check
- This module provides methods for computing
SHA-256
checksums. - en
Encoding
implementations.
Structs§
- Buffer
- A fixed-size buffer for encoding or decoding Crockford’s Base32.
Enums§
- Error
- Error variants for fallible Crockford Base32 operations.
Traits§
- Encoding
- A marker trait for Crockford Base32 variations.
Functions§
- decode
alloc
- Decodes a Crockford Base32-encoded string.
- decode_
check alloc
andcheck
- Decodes a Crockford Base32Check-encoded string.
- decode_
check_ into check
- Decodes Crockford Base32Check-encoded bytes into a provided buffer.
- decode_
check_ prefixed alloc
andcheck
- Decodes a prefixed Crockford Base32Check-encoded string.
- decode_
into - Decodes Crockford Base32-encoded bytes into a provided buffer.
- decode_
prefixed alloc
- Decodes a prefixed Crockford Base32-encoded string.
- decoded_
check_ len check
- Computes the required capacity for decoding from Crockford Base32Check.
- decoded_
len - Computes the required capacity for decoding from Crockford Base32.
- encode
alloc
- Encodes bytes into a Crockford Base32-encoded string.
- encode_
check alloc
andcheck
- Encodes bytes into a Crockford Base32Check-encoded string.
- encode_
check_ into check
- Encodes bytes as Crockford Base32Check into a provided buffer.
- encode_
check_ prefixed alloc
andcheck
- Encodes bytes into a prefixed Crockford Base32Check-encoded string.
- encode_
into - Encodes bytes as Crockford Base32 into a provided buffer.
- encode_
prefixed alloc
- Encodes bytes into a prefixed Crockford Base32-encoded string.
- encoded_
check_ len check
- Computes the required capacity for encoding into Crockford Base32Check.
- encoded_
len - Computes the required capacity for encoding into Crockford Base32.
Type Aliases§
- Result
- Result type for fallible Crockford Base32 operations.