正式准备开写了
This commit is contained in:
parent
c81050d0d8
commit
16d8d3a4ee
20
Cargo.toml
20
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"]
|
||||
|
4
src/log.rs
Normal file
4
src/log.rs
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
pub fn init_logging() {
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
mod file;
|
||||
mod log;
|
||||
mod utils;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
}
|
||||
|
58
src/utils.rs
Normal file
58
src/utils.rs
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user