DEV Community

BC
BC

Posted on

Day20:Compute MD5 of String - 100DayOfRust

Cargo.toml

[dependencies]
md5 = "0.7.0"
Enter fullscreen mode Exit fullscreen mode

Code

use std::fs;

fn main() {
    let content = fs::read_to_string("main.rs").unwrap();
    let hash = md5::compute(content);
    println!("md5: {:?}", hash);
}
Enter fullscreen mode Exit fullscreen mode

Run

Use cargo run get result:

md5: f6c1bbeb1f3a85a44bc550a03f02398e
Enter fullscreen mode Exit fullscreen mode

To verify our code works correctly, we use md5sum shell command to calculate the md5 value:

$ md5sum main.rs
f6c1bbeb1f3a85a44bc550a03f02398e  main.rs
Enter fullscreen mode Exit fullscreen mode

You can see the value is the same as we computed.

Top comments (0)