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.

§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

FeatureDescription
stdImplement std::error::Error for Error
allocAllocation-based API via encode and decode
checkSupport for checksum validation

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

Modules§

checksumcheck
Checksum computation and validation for Crockford Base32Check encoding.

Enums§

Error
Error variants for Crockford Base32 encoding and decoding.

Functions§

decodealloc
Decodes a Crockford Base32-encoded string.
decode_checkcheck and alloc
Decodes a Crockford Base32Check-encoded string.
decode_check_intocheck
Decodes Crockford Base32Check bytes into a provided output buffer.
decode_into
Decodes Crockford Base32-encoded bytes into a provided output buffer.
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_checkcheck and alloc
Encodes bytes into a Crockford Base32Check-encoded string.
encode_check_intocheck
Encodes bytes as Crockford Base32Check into a provided output buffer.
encode_into
Encodes bytes as Crockford Base32 into a provided output buffer.
encoded_check_lencheck
Computes the required capacity for encoding into Crockford Base32Check.
encoded_len
Computes the required buffer capacity for encoding into Crockford Base32.