summaryrefslogtreecommitdiffstats
path: root/src/utils/crypto.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/crypto.rs')
-rw-r--r--src/utils/crypto.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/utils/crypto.rs b/src/utils/crypto.rs
new file mode 100644
index 0000000..9f0ee82
--- /dev/null
+++ b/src/utils/crypto.rs
@@ -0,0 +1,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)
+}