Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from rust-play/playground.rs
Created March 29, 2026 00:18
Show Gist options
  • Select an option

  • Save RandyMcMillan/2e2047e7dce3812c4dd741ec21de7921 to your computer and use it in GitHub Desktop.

Select an option

Save RandyMcMillan/2e2047e7dce3812c4dd741ec21de7921 to your computer and use it in GitHub Desktop.
hash_self.rs
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);
}
@RandyMcMillan
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment