Rust implementation of Crockford's Base32 encoding scheme.
[dependencies]
c32 = "0.6.0"
- 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);
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");
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.
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);
For more details, please refer to the full API Reference.
For security-related concerns, please review the Security Policy. Licensed under Apache License, Version 2.0 or MIT License at your discretion. Contributions to this crate will be dual-licensed under Apache-2.0 and MIT by default, unless specifically indicated otherwise.