src/ContentHash.bend
ber-core-store@0.1.1.0 · 26 lines · 0 definitions
1import Base
2import mylsm-lsm-store@0.3.1.0/src/hub_sha/sha256.bend as ProvenSha
3import bend-codec-lib@0.2.0.0/utf8.bend as Utf8Lib
4import bend-codec-lib@0.2.0.0/bytes.bend as ByteLib
5
6# SHA-256 hex of UTF-8 text. ProvenSha/Utf8 cores are trusted upstream
7# (FIPS 180-4 machine-checked; closed roundtrips proved); this module owns
8# only the byte-list plumbing between them, covered by the differential
9# check vs Python hashlib at the 55/56/64-byte padding boundaries.
10# Unwraps the byte box into the word list the hash core consumes.
11def bytes_data(byte_box: ByteLib.Bytes) -> List<&2, U32>:
12 match byte_box:
13 case ByteLib.Bytes{raw_data}:
14 raw_data
15
16# Hashes valid encodings; unencodable input yields a fixed sentinel string.
17def digest_from_encode_result(encode_result: Result<&2, &2, Utf8Lib.Utf8.Error, ByteLib.Bytes>) -> String:
18 match encode_result:
19 case Fail{encode_error}:
20 "INVALID_UTF8_INPUT"
21 case Done{byte_box}:
22 ProvenSha.hex(ProvenSha.sha256(bytes_data(byte_box)))
23
24# "INVALID_UTF8_INPUT" on unencodable text; never fails on valid input.
25def compute_sha256_hex(input_text: String) -> String:
26 digest_from_encode_result(Utf8Lib.Utf8.encode(input_text))