blob: 9f0ee8202ff539d895601f7707c1d6ea7284a169 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
use base64::{engine::general_purpose::STANDARD, Engine};
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<u8> {
let mut mac = Hmac::<Sha256>::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!("{:02x}", b))
}
pub fn encode_uri(uri: &str) -> String {
uri.split('/')
.map(|v| urlencoding::encode(v))
.collect::<Vec<_>>()
.join("/")
}
pub fn base64_encode<T: AsRef<[u8]>>(input: T) -> String {
STANDARD.encode(input)
}
pub fn base64_decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, base64::DecodeError> {
STANDARD.decode(input)
}
|