Crate c32

Source
Expand description

Crates.io Documentation Build Status License License: MIT

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

FeatureDescription
allocAllocation-based API via encode and decode
checkSupport for checksum validation

For more details, please refer to the full API Reference.

Modules§

checksumcheck
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§

decodealloc
Decodes a Crockford Base32-encoded string.
decode_checkalloc and check
Decodes a Crockford Base32Check-encoded string.
decode_check_intocheck
Decodes Crockford Base32Check-encoded bytes into a provided buffer.
decode_check_prefixedalloc and check
Decodes a prefixed Crockford Base32Check-encoded string.
decode_into
Decodes Crockford Base32-encoded bytes into a provided buffer.
decode_prefixedalloc
Decodes a prefixed Crockford Base32-encoded string.
decoded_check_lencheck
Computes the required capacity for decoding from Crockford Base32Check.
decoded_len
Computes the required capacity for decoding from Crockford Base32.
encodealloc
Encodes bytes into a Crockford Base32-encoded string.
encode_checkalloc and check
Encodes bytes into a Crockford Base32Check-encoded string.
encode_check_intocheck
Encodes bytes as Crockford Base32Check into a provided buffer.
encode_check_prefixedalloc and check
Encodes bytes into a prefixed Crockford Base32Check-encoded string.
encode_into
Encodes bytes as Crockford Base32 into a provided buffer.
encode_prefixedalloc
Encodes bytes into a prefixed Crockford Base32-encoded string.
encoded_check_lencheck
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.