pub struct HashChainAuditor { /* private fields */ }Expand description
Auditor with a hash chain: all logs are linked by SHA-256 chained hashes, supporting tamper detection.
§Tamper-Evidence Mechanism
- Each record’s
current_hash = SHA256(prev_hash || sql || user || timestamp) - The
prev_hashof the next record equals thecurrent_hashof the previous record - Any modification to a historical record changes
current_hash, which then mismatches theprev_hashof the next record - Deleting a middle record breaks the chain; inserting a record changes all subsequent hashes
§Example
use sz_orm_audit::{HashChainAuditor, SqlAuditContext};
let mut auditor = HashChainAuditor::new();
auditor.log(&SqlAuditContext {
sql: "SELECT * FROM users".to_string(),
user: "admin".to_string(),
timestamp: 1000,
});
// Verify chain integrity
assert!(auditor.verify().is_ok());Implementations§
Source§impl HashChainAuditor
impl HashChainAuditor
Sourcepub fn log(&self, ctx: &SqlAuditContext)
pub fn log(&self, ctx: &SqlAuditContext)
Append an audit log to the end of the hash chain.
- If the chain is empty, uses
GENESIS_HASHasprev_hash - Otherwise uses the
current_hashof the last record asprev_hash
The SQL is first masked by mask_sensitive before being written to the
chain, ensuring that the stored audit logs contain no sensitive
information.
Sourcepub fn get_entries(&self) -> Vec<HashChainEntry>
pub fn get_entries(&self) -> Vec<HashChainEntry>
Return a snapshot (clone) of all log entries
Sourcepub fn verify(&self) -> Result<(), String>
pub fn verify(&self) -> Result<(), String>
Verify the integrity of the hash chain.
Checks performed:
- The
prev_hashof the first record equalsGENESIS_HASH - The
current_hashof each record equalscompute_hash(prev_hash, entry) - The
prev_hashof each adjacent record equals thecurrent_hashof the previous record
§Return value
Ok(()): the chain is intact and has not been tampered withErr(reason): the chain has been tampered with;reasondescribes the location and type of the first anomaly
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for HashChainAuditor
impl RefUnwindSafe for HashChainAuditor
impl Send for HashChainAuditor
impl Sync for HashChainAuditor
impl Unpin for HashChainAuditor
impl UnsafeUnpin for HashChainAuditor
impl UnwindSafe for HashChainAuditor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more