10 releases (4 breaking)
new 0.6.1 | Apr 30, 2025 |
---|---|
0.6.0 | Apr 30, 2025 |
0.5.3 | Apr 28, 2025 |
0.4.0 | Feb 10, 2025 |
0.2.1 | Feb 4, 2025 |
#308 in Magic Beans
658 downloads per month
Used in c32address
68KB
945 lines
c32
Rust implementation of Crockford's Base32 encoding scheme.
[dependencies]
c32 = "0.6.0"
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.
Examples
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");
Checksum
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.
Security
For security-related concerns, please review the Security Policy.License
Licensed under Apache License, Version 2.0 or MIT License at your discretion.Contribution
Contributions to this crate will be dual-licensed under Apache-2.0 and MIT by default, unless specifically indicated otherwise.Dependencies
~0–1.1MB