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)
}
#[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())
);
}
}