use base64::{Engine, engine::general_purpose::STANDARD}; use hmac::{Hmac, Mac}; use sha2::{Digest, Sha256}; pub fn sha256(input: &str) -> String { let mut hasher = Sha256::new(); hasher.update(input); format!("{:x}", hasher.finalize()) } pub fn hmac_sha256(key: &[u8], msg: &str) -> Vec { let mut mac = Hmac::::new_from_slice(key).expect("HMAC can take key of any size"); mac.update(msg.as_bytes()); mac.finalize().into_bytes().to_vec() } pub fn hex_encode(bytes: &[u8]) -> String { bytes .iter() .fold(String::new(), |acc, b| acc + &format!("{b:02x}")) } pub fn encode_uri(uri: &str) -> String { uri.split('/') .map(|v| urlencoding::encode(v)) .collect::>() .join("/") } pub fn base64_encode>(input: T) -> String { STANDARD.encode(input) } pub fn base64_decode>(input: T) -> Result, base64::DecodeError> { STANDARD.decode(input) }