https://github.com/52/c32
Rust implementation of Crockford's Base32 encoding
https://github.com/52/c32
base32 crockford stacks-blockchain
Last synced: 4 months ago
JSON representation
Rust implementation of Crockford's Base32 encoding
- Host: GitHub
- URL: https://github.com/52/c32
- Owner: 52
- License: apache-2.0
- Created: 2025-01-25T15:44:48.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-30T15:08:23.000Z (about 1 year ago)
- Last Synced: 2025-11-04T06:25:16.725Z (8 months ago)
- Language: Rust
- Homepage: https://crates.io/crates/c32
- Size: 6.56 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
`c32`
===============
[][Crates.io]
[][Docs.rs]
[][Workflow]
[][License-Apache]
[][License-MIT]
Rust implementation of [Crockford's Base32][Crockford] encoding scheme.
```toml
[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
```rust
let bytes = b"usque ad finem";
let encoded = c32::encode(&bytes);
assert_eq!(encoded, "1TQ6WBNCMG62S10CSMPWSBD");
```
```rust
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:
```rust
// 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");
```
```rust
// 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:
```text
[version (1B)] + [payload (nB)] + [checksum (4B)]
```
And is computed by...
```text
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.
```
```rust
let bytes = b"usque ad finem";
let encoded = c32::encode_check(bytes, 22)?;
assert_eq!(encoded, "P7AWVHENJJ0RB441K6JVK5DNJ7J3V5");
```
```rust
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][Docs.rs].
## 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.
[Crates.io]: https://crates.io/crates/c32
[Docs.rs]: https://docs.rs/c32
[Workflow]: https://github.com/52/c32/actions
[License-Apache]: https://opensource.org/licenses/Apache-2.0
[License-MIT]: https://opensource.org/licenses/MIT
[Crockford]: https://www.crockford.com/base32.html