npub1xu…g43rm on Nostr: //! A crate providing the `get_file_hash!` procedural macro. //! //! This macro ...
//! A crate providing the `get_file_hash!` procedural macro.
//!
//! This macro allows you to compute the SHA-256 hash of a file at compile time,
//! embedding the resulting hash string directly into your Rust executable.
pub use get_file_hash_core::get_file_hash;
/// The SHA-256 hash of this crate's `build.rs` at the time of compilation.
pub const BUILD_HASH: &str = env!("BUILD_HASH");
/// The SHA-256 hash of this crate's `Cargo.toml` at the time of compilation.
pub const CARGO_TOML_HASH: &str = env!("CARGO_TOML_HASH");
/// The SHA-256 hash of this crate's `src/lib.rs` at the time of compilation.
pub const LIB_HASH: &str = env!("LIB_HASH");
/// The name of the package as specified in Cargo.toml.
pub const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME");
/// The version of the package as specified in Cargo.toml.
pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg(feature = "nostr")]
/// The git commit hash of the repository at the time of compilation.
pub const GIT_COMMIT_HASH: &str = env!("GIT_COMMIT_HASH");
#[cfg(feature = "nostr")]
/// The git branch of the repository at the time of compilation.
pub const GIT_BRANCH: &str = env!("GIT_BRANCH");
#[cfg(test)]
mod tests {
use sha2::{Digest, Sha256};
use super::*;
/// Verifies that the exported CARGO_TOML_HASH is not empty.
#[test]
fn test_injected_hash_exists() {
assert!(!BUILD_HASH.is_empty());
println!("Verified build.rs Hash:
{}", BUILD_HASH);
assert!(!CARGO_TOML_HASH.is_empty());
println!("Verified Cargo.toml Hash:
{}", CARGO_TOML_HASH);
assert!(!LIB_HASH.is_empty());
println!("Verified src/lib.rs Hash:\n{}", LIB_HASH);
assert!(!CARGO_PKG_NAME.is_empty());
println!("Verified Package Name:\n{}", CARGO_PKG_NAME);
assert!(!CARGO_PKG_VERSION.is_empty());
println!("Verified Package Version:\n{}", CARGO_PKG_VERSION);
#[cfg(feature = "nostr")]
{
assert!(!GIT_COMMIT_HASH.is_empty());
println!("Verified Git Commit Hash:\n{}", GIT_COMMIT_HASH);
assert!(!GIT_BRANCH.is_empty());
println!("Verified Git Branch:\n{}", GIT_BRANCH);
}
}
/// Tests that the `get_file_hash!` macro correctly computes the SHA-256
/// hash of `lib.rs` and that it matches a manually computed hash of the
/// same file.
#[test]
fn test_get_lib_hash() {
let file_content = include_bytes!("lib.rs");
let mut hasher = Sha256::new();
hasher.update(file_content);
let expected_hash = hasher
.finalize()
.iter()
.map(|b| format!("{:02x}", b))
.collect::<String>();
let actual_hash = get_file_hash!("lib.rs");
assert_eq!(actual_hash, expected_hash);
}
}
Published at
2026-04-04 01:42:47 UTCEvent JSON
{
"id": "21199f585fcb86129cf2fda9bf9d376f56b9e11c19b496c17321dfbf0d3649ee",
"pubkey": "37306250e62aafbe17f1fd1ee25a539f268e044d7516734c6e389c6de7273ac9",
"created_at": 1775266967,
"kind": 1,
"tags": [
[
"file",
"src/lib.rs"
],
[
"version",
"0.3.3"
]
],
"content": "//! A crate providing the `get_file_hash!` procedural macro.\n//!\n//! This macro allows you to compute the SHA-256 hash of a file at compile time,\n//! embedding the resulting hash string directly into your Rust executable.\n\npub use get_file_hash_core::get_file_hash;\n\n/// The SHA-256 hash of this crate's `build.rs` at the time of compilation.\npub const BUILD_HASH: \u0026str = env!(\"BUILD_HASH\");\n\n/// The SHA-256 hash of this crate's `Cargo.toml` at the time of compilation.\npub const CARGO_TOML_HASH: \u0026str = env!(\"CARGO_TOML_HASH\");\n\n/// The SHA-256 hash of this crate's `src/lib.rs` at the time of compilation.\npub const LIB_HASH: \u0026str = env!(\"LIB_HASH\");\n\n/// The name of the package as specified in Cargo.toml.\npub const CARGO_PKG_NAME: \u0026str = env!(\"CARGO_PKG_NAME\");\n\n/// The version of the package as specified in Cargo.toml.\npub const CARGO_PKG_VERSION: \u0026str = env!(\"CARGO_PKG_VERSION\");\n\n#[cfg(feature = \"nostr\")]\n/// The git commit hash of the repository at the time of compilation.\npub const GIT_COMMIT_HASH: \u0026str = env!(\"GIT_COMMIT_HASH\");\n\n#[cfg(feature = \"nostr\")]\n/// The git branch of the repository at the time of compilation.\npub const GIT_BRANCH: \u0026str = env!(\"GIT_BRANCH\");\n\n#[cfg(test)]\nmod tests {\n use sha2::{Digest, Sha256};\n\n use super::*;\n\n /// Verifies that the exported CARGO_TOML_HASH is not empty.\n #[test]\n fn test_injected_hash_exists() {\n assert!(!BUILD_HASH.is_empty());\n println!(\"Verified build.rs Hash:\n{}\", BUILD_HASH);\n\n assert!(!CARGO_TOML_HASH.is_empty());\n println!(\"Verified Cargo.toml Hash:\n{}\", CARGO_TOML_HASH);\n\n assert!(!LIB_HASH.is_empty());\n println!(\"Verified src/lib.rs Hash:\\n{}\", LIB_HASH);\n\n assert!(!CARGO_PKG_NAME.is_empty());\n println!(\"Verified Package Name:\\n{}\", CARGO_PKG_NAME);\n\n assert!(!CARGO_PKG_VERSION.is_empty());\n println!(\"Verified Package Version:\\n{}\", CARGO_PKG_VERSION);\n\n #[cfg(feature = \"nostr\")]\n {\n assert!(!GIT_COMMIT_HASH.is_empty());\n println!(\"Verified Git Commit Hash:\\n{}\", GIT_COMMIT_HASH);\n\n assert!(!GIT_BRANCH.is_empty());\n println!(\"Verified Git Branch:\\n{}\", GIT_BRANCH);\n }\n }\n\n /// Tests that the `get_file_hash!` macro correctly computes the SHA-256\n /// hash of `lib.rs` and that it matches a manually computed hash of the\n /// same file.\n #[test]\n fn test_get_lib_hash() {\n let file_content = include_bytes!(\"lib.rs\");\n\n let mut hasher = Sha256::new();\n hasher.update(file_content);\n let expected_hash = hasher\n .finalize()\n .iter()\n .map(|b| format!(\"{:02x}\", b))\n .collect::\u003cString\u003e();\n\n let actual_hash = get_file_hash!(\"lib.rs\");\n assert_eq!(actual_hash, expected_hash);\n }\n}\n",
"sig": "da8ec18cc5c397fe8cb5c7e920e92a4d5fe7a837a35fe7208109a5faa2750f8e1ed56082e56610c7a359b6e104551996e29320a0bee01fddfac5c07b223cc854"
}