extern crate base64;
extern crate sha2;
use base64::{Engine as _, engine::general_purpose::URL_SAFE};
use rand::{Rng, thread_rng};
use sha2::{Digest, Sha512};
pub fn calculate_hash(value: String) -> String {
let mut hasher = Sha512::new();
hasher.update(value);
let result = hasher.finalize();
format!("{:x}", result)
}
#[allow(dead_code)] pub fn base64_encode(value: &str) -> String {
URL_SAFE.encode(value.as_bytes())
}
pub fn base64_decode(value: &str) -> Result<String, &'static str> {
if let Ok(decoded_bytes) = URL_SAFE.decode(value) {
if let Ok(decoded_str) = String::from_utf8(decoded_bytes) {
return Ok(decoded_str);
} else {
log::error!("Error decoding UTF-8");
}
} else {
log::error!("Error decoding Base64");
}
Err("Server was unable to decrypt the credentials")
}
pub fn hex_encode(value: &str) -> String {
let mut hex_values: Vec<String> = Vec::new();
for ch in value.chars() {
let hex_value = format!("{:04x}", ch as u32);
hex_values.push(hex_value);
}
format!("\\u{}", hex_values.join("\\u"))
}
pub fn hex_decode(value: &str) -> String {
let mut result = String::new();
let hex_values: Vec<&str> = value.split("\\u").skip(1).collect();
for hex_value in hex_values {
if let Ok(code_point) = u32::from_str_radix(hex_value, 16) {
if let Some(ch) = char::from_u32(code_point) {
result.push(ch);
}
}
}
result
}
pub fn keygen() -> String {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
let mut rng = thread_rng();
let token: String = (0..64)
.map(|_| {
let idx = rng.gen_range(0..CHARSET.len());
CHARSET[idx] as char
})
.collect();
token
}