use hmac::crypto_mac::InvalidKeyLength; use hmac::{Hmac, Mac}; use sha2::{Digest, Sha256}; pub type PassKey = (String, Vec); type HmacSha256 = Hmac; /// Hashes a text to a 32 bytes long key. pub fn create_key(pw: &str) -> Vec { let mut hasher = Sha256::default(); hasher.input(pw); let result = hasher.result(); let key = &result[0..8]; key.to_vec().clone() } /// Hashes a text to a sha256. pub fn sha256(pw: &str) -> Vec { let mut hasher = Sha256::default(); hasher.input(pw); let result = hasher.result(); result.to_vec() } /// Creates a sha256 hashsum from the input data pub fn sha_checksum(data: &Vec) -> Vec { let mut hasher = Sha256::default(); hasher.input(data); let result = hasher.result(); result.to_vec() } /// Creates a hmac hash to be appended after the encrypted message pub fn create_hmac(key: &Vec, data: &Vec) -> Result, InvalidKeyLength> { let mut mac = HmacSha256::new_varkey(key)?; mac.input(data); Ok(mac.result().code().to_vec()) }