pub struct HashChainAuditor { /* private fields */ }Expand description
带哈希链的审计器:所有日志通过 SHA-256 链式哈希串联,支持篡改检测。
§防篡改机制
- 每条记录的
current_hash = SHA256(prev_hash || sql || user || timestamp) - 下一条记录的
prev_hash等于上一条的current_hash - 任何对历史记录的修改会导致
current_hash变化, 进而与下一条的prev_hash不匹配 - 删除中间记录会断开链;插入记录会改变后续所有哈希
§示例
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,
});
// 验证链完整性
assert!(auditor.verify().is_ok());Implementations§
Source§impl HashChainAuditor
impl HashChainAuditor
Sourcepub fn log(&self, ctx: &SqlAuditContext)
pub fn log(&self, ctx: &SqlAuditContext)
追加一条审计日志到哈希链末尾。
- 若链为空,使用
GENESIS_HASH作为prev_hash - 否则使用上一条记录的
current_hash作为prev_hash
SQL 会先经过 mask_sensitive 脱敏再写入链中,
确保存储的审计日志不含敏感信息。
Sourcepub fn get_entries(&self) -> Vec<HashChainEntry>
pub fn get_entries(&self) -> Vec<HashChainEntry>
返回所有日志条目的快照(克隆)
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