From 16d8d3a4ee5b7c42f5b1ff8bc8cac3c9602baa7b Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 26 Jan 2024 00:22:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=BC=8F=E5=87=86=E5=A4=87=E5=BC=80?= =?UTF-8?q?=E5=86=99=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 20 ++++++++++++++---- src/file.rs | 0 src/log.rs | 4 ++++ src/main.rs | 5 +++-- src/utils.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 6 deletions(-) delete mode 100644 src/file.rs create mode 100644 src/log.rs create mode 100644 src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index c3b0928..0ab27e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,20 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] -# https://docs.rs/fern/0.6.2/fern/ +[features] +default = [] +db_log = ["db_logger"] -[[fern]] -version = "0.6.2" +[dependencies] + +md-5 = "0.10.6" +sha1 = "0.10.6" + +zstd = "0.13.0" + +log = "0.4.20" +[dependencies.db_logger] +version = "0.1" +optional = true +default-features = false +features = ["postgres"] diff --git a/src/file.rs b/src/file.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/log.rs b/src/log.rs new file mode 100644 index 0000000..99d2950 --- /dev/null +++ b/src/log.rs @@ -0,0 +1,4 @@ + +pub fn init_logging() { + +} diff --git a/src/main.rs b/src/main.rs index 75af312..e287d65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ -mod file; +mod log; +mod utils; fn main() { - println!("Hello, world!"); + } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..6725786 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,58 @@ +use std::path::PathBuf; +use md5::{Md5, Digest}; +use sha1::{Sha1, Digest as Sha1Digest}; + +/// import {join} from 'path' +/// +/// export function hashToFilename(hash: string): string { +/// // eslint-disable-next-line @typescript-eslint/no-magic-numbers +/// return join(hash.substring(0, 2), hash) +/// } +pub fn hash_to_filename(hash: &str) -> PathBuf { + let mut path = PathBuf::new(); + path.push(&hash[0..2]); + path.push(hash); + path +} + +/// import {createHash, Hash} from 'crypto' +/// +/// export function validateFile(buffer: Buffer, checkSum: string): boolean { +/// let hash: Hash +/// if (checkSum.length === 32) { +/// hash = createHash('md5') +/// } else { +/// hash = createHash('sha1') +/// } +/// hash.update(buffer) +/// return hash.digest('hex') === checkSum +/// } +pub fn validate_file(buffer: &[u8], check_sum: &str) -> bool { + match check_sum.len() { + 32 => { + let mut hasher = Md5::new(); + hasher.update(buffer); + let result = hasher.finalize(); + let result_str = format!("{:x}", result); + result_str == check_sum + }, + _ => { + let mut hasher = Sha1::new(); + hasher.update(buffer); + let result = hasher.finalize(); + let result_str = format!("{:x}", result); + result_str == check_sum + } + } +} + +#[test] +fn test_hash_to_filename() { + assert_eq!(hash_to_filename("1234567890abcdef"), PathBuf::from("12/1234567890abcdef")); +} + +#[test] +fn test_validate_file() { + assert_eq!(validate_file(b"hello", "5d41402abc4b2a76b9719d911017c592"), true); + assert_eq!(validate_file(b"hello", "5d41402abc4b2a76b9719d911017c593"), false); +} \ No newline at end of file