npub127…n5r64 on Nostr: //! A simple command-line tool that calculates and displays the SHA-256 hash of //! ...
//! A simple command-line tool that calculates and displays the SHA-256 hash of
//! its own source file.
//!
//! This utility demonstrates how to use the `get_file_hash!` macro to obtain
//! the hash of a specified file at compile time and incorporate it into runtime
//! logic.
use get_file_hash_core::get_file_hash;
use sha2::{Digest, Sha256};
const README_TEMPLATE_PART1: &str = r##"# `get_file_hash` macro
This project provides a Rust procedural macro, `get_file_hash!`, designed to compute the SHA-256 hash of a specified file at compile time. This hash is then embedded directly into your compiled executable. This feature is invaluable for:
* **Integrity Verification:** Ensuring the deployed code hasn't been tampered with.
* **Versioning:** Embedding a unique identifier linked to the exact source code version.
* **Cache Busting:** Generating unique names for assets based on their content.
## Project Structure
* `get_file_hash_core`: A foundational crate containing the `get_file_hash!` macro definition.
* `get_file_hash`: The main library crate that re-exports the macro.
* `src/bin/get_file_hash.rs`: An example executable demonstrating the macro's usage by hashing its own source file and updating this `README.md`.
* `build.rs`: A build script that also utilizes the `get_file_hash!` macro to hash `Cargo.toml` during the build process.
## Usage of `get_file_hash!` Macro
To use the `get_file_hash!` macro, ensure you have `get_file_hash` (or `get_file_hash_core` for direct usage) as a dependency in your `Cargo.toml`.
### Example
```rust
use get_file_hash::get_file_hash;
use sha2::{Digest, Sha256};
fn main() {
// The macro resolves the path relative to CARGO_MANIFEST_DIR
let file_hash = get_file_hash!("src/lib.rs");
println!("The SHA-256 hash of src/lib.rs is: {}", file_hash);
}
```
"##;
const README_TEMPLATE_PART2: &str = r"## Setup and Building
1. **Clone the repository:**
```bash
git clone <repository-url>
cd <repository-name>
```
2. **Build the project:**
```bash
cargo build
```
During the build, `build.rs` will execute and print the hash of `Cargo.toml`.
3. **Run the example executable:**
```bash
cargo run --bin get_file_hash
```
This will print the hash of `src/bin/get_file_hash.rs` to your console.
## Updating this `README.md`
The hash information in this `README.md` is automatically generated by running the example executable.
To update it, execute:
```bash
cargo run --bin get_file_hash > README.md
```
## Current File Hash Information (of `src/bin/get_file_hash.rs`)
* **Target File:** `src/bin/get_file_hash.rs`
";
/// The main entry point of the application.
///
/// This function calculates the SHA-256 hash of the `get_file_hash.rs` source
/// file using a custom procedural macro and then prints the hash to the
/// console. It also includes a basic integrity verification check.
fn main() {
// Calculate the SHA-256 hash of the current file (`get_file_hash.rs`) at
// compile time. The `get_file_hash!` macro reads the file content and
// computes its hash.
let self_hash = get_file_hash!("get_file_hash.rs");
let status_message = if self_hash.starts_with("e3b0") {
"Warning: This hash represents an empty file."
} else {
"Integrity Verified."
};
print!("{}{}", README_TEMPLATE_PART1, README_TEMPLATE_PART2);
println!("* **SHA-256 Hash:** `{}`", self_hash);
println!("* **Status:** {}.\n", status_message);
}
Published at
2026-04-05 05:53:14 UTCEvent JSON
{
"id": "b0133f6a8496af93d6372eadf1f64f5f38d8a81f3ccba15797f9c85844f8f031",
"pubkey": "57b9455391febd50ebf57e3cae542b3339bba7afbf2a12484b98dde60421db06",
"created_at": 1775368394,
"kind": 1,
"tags": [
[
"file",
"src/bin/get_file_hash.rs"
],
[
"version",
"0.4.7"
]
],
"content": "//! A simple command-line tool that calculates and displays the SHA-256 hash of\n//! its own source file.\n//!\n//! This utility demonstrates how to use the `get_file_hash!` macro to obtain\n//! the hash of a specified file at compile time and incorporate it into runtime\n//! logic.\n\nuse get_file_hash_core::get_file_hash;\nuse sha2::{Digest, Sha256};\n\nconst README_TEMPLATE_PART1: \u0026str = r##\"# `get_file_hash` macro\n\nThis project provides a Rust procedural macro, `get_file_hash!`, designed to compute the SHA-256 hash of a specified file at compile time. This hash is then embedded directly into your compiled executable. This feature is invaluable for:\n\n* **Integrity Verification:** Ensuring the deployed code hasn't been tampered with.\n* **Versioning:** Embedding a unique identifier linked to the exact source code version.\n* **Cache Busting:** Generating unique names for assets based on their content.\n\n## Project Structure\n\n* `get_file_hash_core`: A foundational crate containing the `get_file_hash!` macro definition.\n* `get_file_hash`: The main library crate that re-exports the macro.\n* `src/bin/get_file_hash.rs`: An example executable demonstrating the macro's usage by hashing its own source file and updating this `README.md`.\n* `build.rs`: A build script that also utilizes the `get_file_hash!` macro to hash `Cargo.toml` during the build process.\n\n## Usage of `get_file_hash!` Macro\n\nTo use the `get_file_hash!` macro, ensure you have `get_file_hash` (or `get_file_hash_core` for direct usage) as a dependency in your `Cargo.toml`.\n\n### Example\n\n```rust\nuse get_file_hash::get_file_hash;\nuse sha2::{Digest, Sha256};\n\nfn main() {\n // The macro resolves the path relative to CARGO_MANIFEST_DIR\n let file_hash = get_file_hash!(\"src/lib.rs\");\n println!(\"The SHA-256 hash of src/lib.rs is: {}\", file_hash);\n}\n```\n\n\"##;\n\nconst README_TEMPLATE_PART2: \u0026str = r\"## Setup and Building\n\n1. **Clone the repository:**\n ```bash\n git clone \u003crepository-url\u003e\n cd \u003crepository-name\u003e\n ```\n2. **Build the project:**\n ```bash\n cargo build\n ```\n During the build, `build.rs` will execute and print the hash of `Cargo.toml`.\n3. **Run the example executable:**\n ```bash\n cargo run --bin get_file_hash\n ```\n This will print the hash of `src/bin/get_file_hash.rs` to your console.\n\n## Updating this `README.md`\n\nThe hash information in this `README.md` is automatically generated by running the example executable.\nTo update it, execute:\n\n```bash\ncargo run --bin get_file_hash \u003e README.md\n```\n\n## Current File Hash Information (of `src/bin/get_file_hash.rs`)\n\n* **Target File:** `src/bin/get_file_hash.rs`\n\";\n\n/// The main entry point of the application.\n///\n/// This function calculates the SHA-256 hash of the `get_file_hash.rs` source\n/// file using a custom procedural macro and then prints the hash to the\n/// console. It also includes a basic integrity verification check.\nfn main() {\n // Calculate the SHA-256 hash of the current file (`get_file_hash.rs`) at\n // compile time. The `get_file_hash!` macro reads the file content and\n // computes its hash.\n let self_hash = get_file_hash!(\"get_file_hash.rs\");\n\n let status_message = if self_hash.starts_with(\"e3b0\") {\n \"Warning: This hash represents an empty file.\"\n } else {\n \"Integrity Verified.\"\n };\n\n print!(\"{}{}\", README_TEMPLATE_PART1, README_TEMPLATE_PART2);\n println!(\"* **SHA-256 Hash:** `{}`\", self_hash);\n println!(\"* **Status:** {}.\\n\", status_message);\n}\n",
"sig": "1065d7609b5c8dcda448718024337ef60684068deca2077dbb9214e997a21976981b0026dce03b7d33b2d1a69a229b85601a2ce28edac2b10d772f8eeda71d2d"
}