summaryrefslogtreecommitdiffstats
path: root/src/hash.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/hash.rs')
-rw-r--r--src/hash.rs30
1 files changed, 9 insertions, 21 deletions
diff --git a/src/hash.rs b/src/hash.rs
index 0321437..70c3791 100644
--- a/src/hash.rs
+++ b/src/hash.rs
@@ -1,6 +1,3 @@
-//! Hashing helpers. `sha256` hex strings are the currency of identity and
-//! provenance throughout the system (DESIGN.md §3, §5.3).
-
use sha2::{Digest, Sha256};
use std::io::{self, Read, Write};
@@ -22,10 +19,9 @@ pub fn short(bytes: &[u8], n: usize) -> String {
h[..n.min(h.len())].to_string()
}
-/// Stream-hash a reader, optionally teeing every byte into `sink`.
-///
-/// Used to hash a download while writing it into the source cache, so the
-/// bandwidth is paid for once (§5.3 "stream-hash, don't buffer").
+/// Stream-hash a reader, optionally teeing every byte into `sink` — so a
+/// download can be hashed into the source cache without paying for the
+/// bandwidth twice (§5.3).
pub fn digest_reader<R: Read>(mut r: R, mut sink: Option<&mut dyn Write>) -> io::Result<(String, u64)> {
let mut h = Sha256::new();
let mut buf = vec![0u8; 64 * 1024];
@@ -50,11 +46,8 @@ pub fn digest_file(path: &std::path::Path) -> io::Result<(String, u64)> {
digest_reader(&mut br, None)
}
-/// Hash of a canonical, sorted list of `(path, blob_hash)` pairs.
-///
/// This is `recipe_hash` (DESIGN.md §3): a digest over *every* file in the AUR
-/// snapshot tree — PKGBUILD, .SRCINFO, *.install and any shipped patches or
-/// assets — because a patch file shipped in the AUR repo is part of the recipe
+/// snapshot tree, because a patch shipped in the AUR repo is part of the recipe
/// and is a classic silent-change vector.
pub fn recipe_hash(entries: &[(String, String)]) -> String {
let mut sorted: Vec<&(String, String)> = entries.iter().collect();
@@ -69,17 +62,12 @@ pub fn recipe_hash(entries: &[(String, String)]) -> String {
hex::encode(h.finalize())
}
-/// Serialize a value to canonical JSON: compact, with every object's keys in
-/// sorted order, so equal values always produce equal bytes.
-///
-/// The ordering is not free in `serde_json` — a struct serializes in field
-/// declaration order and a map in whatever order it happens to hold — so the
-/// value is routed through `serde_json::Value`, whose map is a `BTreeMap`.
-/// That makes the name true rather than aspirational.
+/// Serialize to canonical JSON: compact, object keys sorted, so equal values always
+/// produce equal bytes. The value is routed through `serde_json::Value` because a
+/// struct otherwise serializes in field declaration order.
///
-/// A serialization failure is an error. It used to be `"{}"`, which meant every
-/// value that failed to serialize hashed to the same digest — and this is the
-/// function behind `Identity::hash`, the value an approval echoes (§6 M-2).
+/// A serialization failure is an error, not `"{}"`: this is the function behind
+/// `Identity::hash` (M-2).
pub fn canonical_json<T: serde::Serialize>(v: &T) -> Result<String, serde_json::Error> {
serde_json::to_string(&serde_json::to_value(v)?)
}