Add SHA-256 to proto, wrapping the emsha crate

Implemented-By: Laguna S 2.1 (OpenCode)
This commit is contained in:
2026-09-17 14:11:51 -07:00
parent db0ba081db
commit bc1e727c19
8 changed files with 156 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
use crate::Hash32;
use emsha::Hash as _;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HashError;
impl std::fmt::Display for HashError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "sha256 error")
}
}
impl std::error::Error for HashError {}
pub struct Sha256(emsha::sha256::SHA256);
impl Sha256 {
pub fn new() -> Self {
Sha256(emsha::sha256::SHA256::new())
}
pub fn update(&mut self, data: &[u8]) -> Result<(), HashError> {
self.0.update(data).map_err(|_| HashError)
}
pub fn finish(mut self) -> Result<Hash32, HashError> {
let mut out = [0u8; emsha::sha256::SIZE];
self.0.finalize(&mut out).map_err(|_| HashError)?;
Ok(Hash32::from_bytes(out))
}
}
impl Default for Sha256 {
fn default() -> Self {
Self::new()
}
}
pub fn sha256(data: &[u8]) -> Result<Hash32, HashError> {
let mut h = Sha256::new();
h.update(data)?;
h.finish()
}
+2
View File
@@ -4,6 +4,7 @@ pub mod audit;
pub mod class;
pub mod frame;
pub mod grant;
pub mod hash;
pub mod ids;
pub mod log;
pub mod wire;
@@ -12,6 +13,7 @@ pub use audit::{AuditRecord, DecisionRecord};
pub use class::DataClass;
pub use frame::{FrameError, MAX_FRAME, read_frame, write_frame};
pub use grant::{Constraints, Grant, Mode};
pub use hash::{HashError, Sha256, sha256};
pub use ids::{CallId, Epoch, Hash32, SessionId, Timestamp, ValueError};
pub use log::{LogRecord, ToolCall};
pub use wire::{