[][src]Crate bech32

Encoding and decoding Bech32 format

Bech32 is a 5-bit (base-32) encoding scheme that produces strings that comprise a human-readable part, a separator, a data part, and a checksum. The encoding implements a BCH code that guarantees error detection of up to four characters with less than 1 in 1 billion chance of failing to detect more errors.

The Bech32 encoding was originally formulated in BIP-0173

Examples

use bech32::{self, FromBase32, ToBase32};

let encoded = bech32::encode("bech32", vec![0x00, 0x01, 0x02].to_base32()).unwrap();
assert_eq!(encoded, "bech321qqqsyrhqy2a".to_string());

let (hrp, data) = bech32::decode(&encoded).unwrap();
assert_eq!(hrp, "bech32");
assert_eq!(Vec::<u8>::from_base32(&data).unwrap(), vec![0x00, 0x01, 0x02]);

Structs

Bech32Writer

Allocationless Bech32 writer that accumulates the checksum data internally and writes them out in the end.

u5

Integer in the range 0..32

Enums

Error

Error types for Bech32 encoding / decoding

Traits

Base32Len

Interface to calculate the length of the base32 representation before actually serializing

CheckBase32

A trait to convert between u8 arrays and u5 arrays without changing the content of the elements, but checking that they are in range.

FromBase32

Parse/convert base32 slice to Self. It is the reciprocal of ToBase32.

ToBase32

A trait for converting a value to a type T that represents a u5 slice.

WriteBase32

Interface to write u5s into a sink

Functions

convert_bits

Convert between bit sizes

decode

Decode a bech32 string into the raw HRP and the data bytes.

encode

Encode a bech32 payload to string.

encode_to_fmt

Encode a bech32 payload to an fmt::Write. This method is intended for implementing traits from std::fmt.