Skip to content

base64_decode

Decodes s from base64 (RFC 4648 section 4).

use helpers4::hex::base64_decode;

Cargo feature hex (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features hex

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.7", default-features = false, features = ["hex"] }
pub fn base64_decode(s: &str) -> Result<Vec<u8>, Base64DecodeError>
ParameterTypeDescription
s&strThe base64 string to decode, with = padding to a multiple of 4 characters.

Result<Vec<u8>, Base64DecodeError> — Ok on success, otherwise an Err: see Errors.

Returns a Base64DecodeError when the length is not a multiple of 4, = appears anywhere but the end, or a character is not in the base64 alphabet.

use helpers4::hex::base64_decode;

assert_eq!(base64_decode("TWFu").as_deref(), Ok(&b"Man"[..]));
assert!(base64_decode("TWFu!").is_err()); // not a multiple of 4

Why a string could not be decoded as base64.

use helpers4::hex::Base64DecodeError;

#[non_exhaustive]
pub enum Base64DecodeError {
    /// The string's length is not a multiple of 4.
    InvalidLength {
        /// Actual number of characters.
        actual: usize,
    },
    /// A `=` padding character appears somewhere other than the end of the string.
    InvalidPadding,
    /// A character that is not in the base64 alphabet (`A`-`Z`, `a`-`z`, `0`-`9`, `+`, `/`) or a
    /// trailing `=`.
    InvalidChar {
        /// Byte offset of the character in the input.
        index: usize,
        /// The offending character.
        found: char,
    },
}

src/hex/base64_decode.rs