正式准备开写了

This commit is contained in:
shenjack 2024-01-26 00:22:23 +08:00
parent c81050d0d8
commit 16d8d3a4ee
Signed by: shenjack
GPG Key ID: 7B1134A979775551
5 changed files with 81 additions and 6 deletions

View File

@ -5,8 +5,20 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [features]
# https://docs.rs/fern/0.6.2/fern/ default = []
db_log = ["db_logger"]
[[fern]] [dependencies]
version = "0.6.2"
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"]

View File

4
src/log.rs Normal file
View File

@ -0,0 +1,4 @@
pub fn init_logging() {
}

View File

@ -1,5 +1,6 @@
mod file; mod log;
mod utils;
fn main() { fn main() {
println!("Hello, world!");
} }

58
src/utils.rs Normal file
View File

@ -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);
}