Header Ads

How to hash files on rust

how to hash files on rust

how to hash files on rust

We use cargo to build this example:
Require modules:

sha2 = "0.10.0"

Code example:

//src/main.rs

use sha2::{Digest, Sha256};
use std::{fs, io};

pub fn main() {
let mut file = fs::File::open("./src/hash/index.html").unwrap();

// create a Sha256 object
let mut hasher = Sha256::new();

let n = io::copy(&mut file, &mut hasher);

// read hash digest and consume hasher
let result = hasher.finalize();

println!("Bytes processed: {:?}", n);

println!("Hash: {:x}", result);
}


//src/index.html

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Document</title>
</head>

<body>
<h1>Dylan Ngo</h1>
<div>
<p>Hash this file</p>
</div>
</body>

</html>

Thanks.


No comments