#模糊 #哈希 #ssdeep

fuzzyhash

纯 Rust 模糊哈希实现

8 个版本

0.2.2 2023年3月13日
0.2.1 2021年1月13日
0.2.0 2020年1月28日
0.1.4 2018年1月31日
0.1.1 2017年4月24日

算法 分类中排名第 191

Download history 244/week @ 2024-03-13 284/week @ 2024-03-20 179/week @ 2024-03-27 204/week @ 2024-04-03 168/week @ 2024-04-10 346/week @ 2024-04-17 417/week @ 2024-04-24 308/week @ 2024-05-01 337/week @ 2024-05-08 266/week @ 2024-05-15 132/week @ 2024-05-22 312/week @ 2024-05-29 355/week @ 2024-06-05 228/week @ 2024-06-12 117/week @ 2024-06-19 138/week @ 2024-06-26

每月下载量 863
6 个包中(5 个直接)使用

MIT 许可证

32KB
635

fuzzyhash-rs

Build Status Documentation

纯 Rust 模糊哈希实现。

用法

哈希文件

use fuzzyhash::FuzzyHash;

let fuzzy = FuzzyHash::file("/path/to/file").unwrap();

// `FuzzyHash` implements `Display` so this works:

println!("fuzzy hash of file: {}", fuzzy);

哈希数据

use fuzzyhash::FuzzyHash;

// Anything that implements `AsRef<[u8]>` can be immediately hashed

let data = vec![1,2,3,4,5,6,7,8,9,10];

let fuzzy = FuzzyHash::new(data);

任何实现了 std::io::Read 的内容

use fuzzyhash::FuzzyHash;
use std::io::{Cursor, Read};

let mut cursor = Cursor::new(vec![1,2,3,4,5]);
let fuzzy = FuzzyHash::read(&mut cursor);

手动从数据块构建模糊哈希

use fuzzyhash::FuzzyHash;
use std::io::Read;

let mut file = std::fs::File::open("/path/to/my/file").unwrap();
let mut fuzzy_hash = FuzzyHash::default();

loop {
    let mut buffer = vec![0; 1024];
    let count = file.read(&mut buffer).unwrap();

    fuzzy_hash.update(buffer);

if count < 1024 {
        break;
    }
}

fuzzy_hash.finalize();

println!("Fuzzy hash of data: {}", fuzzy_hash);

FFI 兼容性 两个函数提供了此库的 FFI 使用入口点。

// hashing some data
unsigned char *data = (unsigned char*)malloc(256);
// fill this buffer...
int fuzzy = fuzzyhash(data, 256);
// compare two fuzzyhashes
char *first = "96:U57GjXnLt9co6pZwvLhJluvrszNgMFwO6MFG8SvkpjTWf:Hj3BeoEcNJ0TspgIG8SvkpjTg";
char *second = "96:U57GjXnLt9co6pZwvLhJluvrs1eRTxYARdEallia:Hj3BeoEcNJ0TsI9xYeia3R";
int compared = fuzzyhash_compare(first, second);

状态

目前,此库仅支持 ssdeep 模糊哈希算法的 None 模式,最终将实现 EliminateSequencesDoNotTruncate

运行示例

$ cargo run -q --example example1 /bin/bash
24576:z0wp2rLW2W2iYQK+q/VjsFEDe866QHX4kC:rp2rLW2W2iYJ+FEg6QHX

0.2.0 API 变更

库的公共 API 已被大量重新构想,并包含大量破坏性变更。

0.1.3 更新

修复了由于不必要地克隆大缓冲区而导致的性能瓶颈(约 22% 更快)。

1000 次大型随机缓冲区迭代

0.1.2:

$ time cargo bench
    Finished release [optimized] target(s) in 0.0 secs
     Running target/release/deps/fuzzyhash-a709fbd8d1125c4f

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/release/deps/random_data1-6d3edf5ebe8a1b5f

running 1 test
test hashing_bench ... bench: 111,144,101 ns/iter (+/- 2,712,598)

test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured; 0 filtered out


real    0m33.786s
user    0m33.757s
sys     0m0.030s

0.1.3:

$ time cargo bench
    Finished release [optimized] target(s) in 0.0 secs
     Running target/release/deps/fuzzyhash-9ad0dfdb1b3b0386

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/release/deps/random_data1-3bec1fdd42a47a95

running 1 test
test hashing_bench ... bench:  87,273,582 ns/iter (+/- 2,535,966)

test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured; 0 filtered out


real    0m26.525s
user    0m26.515s
sys     0m0.011s

致谢

我之前已将算法移植到 C++(fuzzypp),但找不到 Rust 版本,所以就有了这个!我一定要提一下 kolos450 将算法移植到 C# 的工作,这为我的两个实现都提供了很好的起点。

无运行时依赖