summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/crypto.rs35
-rw-r--r--src/utils/mod.rs10
2 files changed, 37 insertions, 8 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)
+}
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 771baf3..86d84d8 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -1,5 +1,6 @@
mod abort_signal;
mod clipboard;
+mod crypto;
mod prompt_input;
mod render_prompt;
mod spinner;
@@ -7,6 +8,7 @@ mod tiktoken;
pub use self::abort_signal::{create_abort_signal, AbortSignal};
pub use self::clipboard::set_text;
+pub use self::crypto::*;
pub use self::prompt_input::*;
pub use self::render_prompt::render_prompt;
pub use self::spinner::run_spinner;
@@ -14,7 +16,6 @@ pub use self::tiktoken::cl100k_base_singleton;
use fancy_regex::Regex;
use lazy_static::lazy_static;
-use sha2::{Digest, Sha256};
use std::env;
use std::process::Command;
@@ -82,13 +83,6 @@ pub fn light_theme_from_colorfgbg(colorfgbg: &str) -> Option<bool> {
Some(light)
}
-pub fn sha256sum(input: &str) -> String {
- let mut hasher = Sha256::new();
- hasher.update(input);
- let result = hasher.finalize();
- format!("{:x}", result)
-}
-
pub fn detect_os() -> String {
let os = env::consts::OS;
if os == "linux" {