Crates.io | ed2k |
lib.rs | ed2k |
version | 1.0.1 |
source | src |
created_at | 2023-05-19 21:54:49.710201 |
updated_at | 2023-05-19 22:55:00.744168 |
description | An implementation of the ED2K hash function |
homepage | |
repository | https://github.com/Kimundi/ed2k |
max_upload_size | |
id | 869115 |
size | 28,051 |
This implements multiple flavors of the ED2K hash function as described on https://mldonkey.sourceforge.net/Ed2k-hash, and verfied against other implementations.
The Algorithm roughly works as follows:
It turned out that when the ED2K algorithm was originally released, there was an ambiguity in its description that allowed two ways to implement it, which caused diverging implementations in tools. The ambiguity seems to have been resolved later on, with the alternative behavior now being regarded as a bug and fixed in reference implementations.
This crate implements both forms, as well as an efficient way to compute both at once:
use digest::Digest;
use ed2k::{Ed2k, Ed2kRed, Ed2kBlue};
// Compute a ED2K hash.
let hash = Ed2k::digest(b"hello world");
assert_eq!(format!("{hash:x}"), "aa010fbc1d14c795d86ef98c95479d17");
// Difference between blue and red flavors.
// The two only differ for data that ends on a chunk boundary.
let data = vec![0x55; 9728000];
let red_hash = Ed2kRed::digest(&data);
let blue_hash = Ed2kBlue::digest(&data);
assert_eq!(format!("{red_hash:x}"), "49e80f377b7e4e706dbd3ecc89f39306");
assert_eq!(format!("{blue_hash:x}"), "4127a47867b6110f0f86f2d9845fb374");