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
unsafeblocks.
§Examples
§std or alloc
// encoding...
let bytes = b"usque ad finem";
let encoded = c32::encode(&bytes);
assert_eq!(encoded, "1TQ6WBNCMG62S10CSMPWSBD");// decoding...
let bytes = b"usque ad finem";
let decoded = c32::decode("1TQ6WBNCMG62S10CSMPWSBD")?;
assert_eq!(decoded, bytes);§#![no_std]
// encoding...
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...
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 (check)
The check feature provides 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
§std or alloc
// encoding...
let bytes = b"usque ad finem";
let encoded = c32::encode_check(bytes, 22)?;
assert_eq!(encoded, "P7AWVHENJJ0RB441K6JVK5DNJ7J3V5");// decoding...
let encoded = "P7AWVHENJJ0RB441K6JVK5DNJ7J3V5";
let (version, decoded) = c32::decode_check(encoded)?;
assert_eq!(decoded, b"usque ad finem");
assert_eq!(version, 22);§#![no_std]
// encoding...
let bytes = b"usque ad finem";
let mut buffer = [0; 32];
let written = c32::encode_check_into(bytes, 22, &mut buffer)?;
let encoded = &buffer[..written];
assert_eq!(encoded, b"P7AWVHENJJ0RB441K6JVK5DNJ7J3V5");// decoding...
let encoded = b"P7AWVHENJJ0RB441K6JVK5DNJ7J3V5";
let mut buffer = [0; 32];
let (version, written) = c32::decode_check_into(encoded, &mut buffer)?;
let decoded = &buffer[..written];
assert_eq!(decoded, b"usque ad finem");
assert_eq!(version, 22);§Features
| Feature | Description |
|---|---|
std | Implement std::error::Error for Error |
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 - Checksum computation and validation for Crockford Base32Check encoding.
Enums§
- Error
- Error variants for Crockford Base32 encoding and decoding.
Functions§
- decode
alloc - Decodes a Crockford Base32-encoded string.
- decode_
check checkandalloc - Decodes a Crockford Base32Check-encoded string.
- decode_
check_ into check - Decodes Crockford Base32Check bytes into a provided output buffer.
- decode_
into - Decodes Crockford Base32-encoded bytes into a provided output buffer.
- 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 checkandalloc - Encodes bytes into a Crockford Base32Check-encoded string.
- encode_
check_ into check - Encodes bytes as Crockford Base32Check into a provided output buffer.
- encode_
into - Encodes bytes as Crockford Base32 into a provided output buffer.
- encoded_
check_ len check - Computes the required capacity for encoding into Crockford Base32Check.
- encoded_
len - Computes the required buffer capacity for encoding into Crockford Base32.