Skip to main content

FromBase32Str

Trait FromBase32Str 

Source
pub trait FromBase32Str {
    // Required method
    fn try_from_base32(&self) -> Result<Vec<u8>, Base32Error>;
}
Available on crate features alloc and encoding-base32 only.
Expand description

Extension trait for decoding uppercase, unpadded Base32 strings into byte vectors.

Requires features encoding-base32 and alloc.

Blanket-implemented for all AsRef<str> types. Returns Vec<u8> — requires heap allocation. For no-alloc targets, use Fixed::try_from_base32 instead, which decodes directly into a stack-allocated [u8; N] buffer.

Uses the RFC 4648 §6 alphabet (A–Z, 2–7) without = padding.

The returned Vec<u8> is plain heap memory and is not zeroized on drop. Wrap the result in Fixed or Dynamic immediately (or in zeroize::Zeroizing) if the decoded bytes are sensitive. Prefer Fixed::try_from_base32 / Dynamic::try_from_base32, which perform the wrapping for you and zeroize their internal temporaries.

Treat all input as untrusted; validate lengths and content upstream before wrapping decoded bytes in secrets.

Required Methods§

Source

fn try_from_base32(&self) -> Result<Vec<u8>, Base32Error>

Decodes an uppercase, unpadded Base32 string into a byte vector.

§Errors
  • Base32Error::InvalidBase32 — wrong alphabet or case, = padding, whitespace, or a length that is impossible for unpadded Base32.
§Examples
use secure_gate::FromBase32Str;

// "NBSWY3DP" decodes to b"hello"
let bytes = "NBSWY3DP".try_from_base32()?;
assert_eq!(bytes, b"hello");

assert!("nbswy3dp".try_from_base32().is_err()); // lowercase
assert!("MY======".try_from_base32().is_err()); // padding
assert!("!!!!".try_from_base32().is_err()); // invalid chars

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§