#md5 #hash #hashing #data #io-read #compute #algorithm

yet-another-md5

A library to compute md5 hashes from Read objects

2 stable releases

2.0.0 Mar 13, 2024
1.0.0 Feb 17, 2024

#1550 in Algorithms

Download history 125/week @ 2024-02-12 26/week @ 2024-02-19 20/week @ 2024-02-26 17/week @ 2024-03-04 135/week @ 2024-03-11 7/week @ 2024-03-18 35/week @ 2024-04-01

178 downloads per month

MIT license

31KB
538 lines

yet-another-md5

An implementation of the MD5 hash algorithm capable to hash data readed from a std::io::Read implementation.

Why?

The main motivation of this project is as an exercise to learn some Rust.

The second one is that the MD5 implementations I found for Rust hashes binary strings, which forced you to have the whole data to hash in memory; this looked silly to me given that the MD5 algorithm hash the data in chunks.

Usage

use std::fs::File;
use std::io::prelude::*;
use ya_md5::Md5Hasher;
use ya_md5::Hash;
use ya_md5::Md5Error;

fn main() -> Result<(), Md5Error> {
    let mut file = File::open("foo.txt")?;
    Md5Hasher::hash(&mut file)?
    let result = format!("{}", hash);
    assert_eq!(result, "5eb63bbbe01eeed093cb22bb8f5acdc3");
    Ok(())
}

See the docs.


lib.rs:

An implementation of the MD5 hash algorithm capable to hash data readed from a std::io::Read implementation.

Example

use std::fs::File;
use std::io::prelude::*;
use ya_md5::Md5Hasher;
use ya_md5::Hash;
use ya_md5::Md5Error;

fn example() -> Result<(), Md5Error> {
    std::fs::write("foo.txt", b"hello world")?;
    let hash = {
        let mut file = File::open("foo.txt")?;
        Md5Hasher::hash(&mut file)?
    };
    std::fs::remove_file("foo.txt")?;
    let result = format!("{}", hash);
    assert_eq!(result, "5eb63bbbe01eeed093cb22bb8f5acdc3");
    Ok(())
}

Dependencies

~0.4–0.9MB
~21K SLoC