2024-09-05 23:51:08 +00:00
|
|
|
use base64::engine::{DecodePaddingMode, GeneralPurpose as Base64Engine, GeneralPurposeConfig};
|
2024-09-14 00:33:23 +00:00
|
|
|
use base64::{alphabet, Engine};
|
|
|
|
|
|
|
|
pub use base64::DecodeError;
|
2024-09-05 23:51:08 +00:00
|
|
|
|
|
|
|
/// Don't add padding when encoding, and require no padding when decoding.
|
|
|
|
pub const ENGINE: Base64Engine = Base64Engine::new(
|
|
|
|
&alphabet::STANDARD,
|
|
|
|
GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent),
|
|
|
|
);
|
|
|
|
|
|
|
|
pub fn encode<T>(data: T) -> String
|
|
|
|
where
|
|
|
|
T: AsRef<[u8]>,
|
|
|
|
{
|
|
|
|
ENGINE.encode(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decode<T>(data: T) -> Result<Vec<u8>, DecodeError>
|
|
|
|
where
|
|
|
|
T: AsRef<[u8]>,
|
|
|
|
{
|
|
|
|
ENGINE.decode(data)
|
|
|
|
}
|