use ::base64::{engine::general_purpose, DecodeError, Engine as _};
pub fn encode(data: &[u8]) -> String {
general_purpose::STANDARD.encode(data)
}
pub fn decode(data: &str) -> Result<Vec<u8>, DecodeError> {
general_purpose::STANDARD.decode(data)
}
pub fn encode_b64_wrapped(data: &[u8]) -> String {
format!("b64'{}'", encode(data))
}
pub fn decode_b64_wrapped(value: &str) -> Option<Vec<u8>> {
let inner = value.strip_prefix("b64'")?.strip_suffix('\'')?;
decode(inner).ok()
}
#[cfg(test)]
mod tests {
#![allow(clippy::expect_used)]
#![allow(clippy::panic)]
#![allow(clippy::unwrap_used)]
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
use wasm_bindgen_test::wasm_bindgen_test;
use crate::crypto::base64;
#[test]
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test
)]
fn encode() {
assert_eq!(base64::encode(b"Hello, world"), "SGVsbG8sIHdvcmxk");
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test
)]
fn decode() {
assert_eq!(
base64::decode("SGVsbG8sIHdvcmxk"),
Ok(b"Hello, world".to_vec())
);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test
)]
fn encode_b64_wrapped() {
assert_eq!(
base64::encode_b64_wrapped(b"Hello, world"),
"b64'SGVsbG8sIHdvcmxk'"
);
}
#[test]
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test
)]
fn decode_b64_wrapped_roundtrip() {
let wrapped = base64::encode_b64_wrapped(b"Hello, world");
assert_eq!(
base64::decode_b64_wrapped(&wrapped),
Some(b"Hello, world".to_vec())
);
assert_eq!(base64::decode_b64_wrapped("not wrapped"), None);
}
}