Оно работает!

Почищу код позже...
This commit is contained in:
Shiroyasha 2024-05-17 11:00:37 +03:00
parent 3fd49300db
commit b280da2742
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
14 changed files with 659 additions and 114 deletions

59
src/utils.rs Normal file
View file

@ -0,0 +1,59 @@
use std::{fs::File, io::Read};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use ring::digest::{self, digest};
use uuid::Uuid;
use base64::prelude::*;
// Кор функции
pub fn rand() -> [u8; 50] {
let mut rng = thread_rng();
let distr = rand::distributions::Uniform::new_inclusive(0, 255);
let mut nums: [u8; 50] = [0u8; 50];
for x in &mut nums {
*x = rng.sample(distr);
}
nums
}
pub fn bytes_into_string(code: &[u8]) -> String {
code.iter().map(|byte| format!("{:02x}", byte)).collect::<String>()
}
// Конец кор функций
pub fn _generate_hex_string(length: usize) -> String { // FIXME: Variable doesn't using!
let rng = thread_rng();
let random_bytes: Vec<u8> = rng
.sample_iter(&Alphanumeric)
.take(length / 2)
.collect();
hex::encode(random_bytes)
}
pub fn format_uuid(uuid: Uuid) -> String {
// let uuid = Uuid::parse_str(&uuid)?; TODO: Вероятно format_uuid стоит убрать
// .map_err(|_| tide::Error::from_str(StatusCode::InternalServerError, "Failed to parse UUID"))?;
uuid.as_hyphenated().to_string()
}
pub fn calculate_file_sha256(file_path: &str) -> Result<String, std::io::Error> {
// Read the file content
let mut file = File::open(file_path)?;
let mut content = Vec::new();
file.read_to_end(&mut content)?;
// Convert the content to base64
let base64_content = BASE64_STANDARD.encode(&content);
// Calculate the SHA-256 hash of the base64 string
let binding = digest(&digest::SHA256, base64_content.as_bytes());
let hash = binding.as_ref();
// Convert the hash to a hexadecimal string
let hex_hash = bytes_into_string(hash);
Ok(hex_hash)
}