-
-
Save RandyMcMillan/2e2047e7dce3812c4dd741ec21de7921 to your computer and use it in GitHub Desktop.
hash_self.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use sha2::{Sha256, Digest}; | |
| use std::time::Instant; | |
| fn main() { | |
| // include_bytes! embeds the source code at compile time. | |
| let src_bytes = include_bytes!("main.rs"); | |
| let start_time = Instant::now(); | |
| // Initialize the SHA-256 hasher | |
| let mut hasher = Sha256::new(); | |
| hasher.update(src_bytes); | |
| // Finalize the hash | |
| let result = hasher.finalize(); | |
| let elapsed = start_time.elapsed(); | |
| // Print the result by converting each byte to a hex string | |
| print!("SHA-256 Hash of main.rs: "); | |
| for byte in result { | |
| print!("{:02x}", byte); | |
| } | |
| println!("\nFile size: {} bytes", src_bytes.len()); | |
| println!("Hash computed in: {:?}", elapsed); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=827feffbc03afb37079e2b2513e1c3fb