Class: Beni::Vendor::Checksum
- Inherits:
-
Object
- Object
- Beni::Vendor::Checksum
- Defined in:
- lib/beni/vendor/checksum.rb,
sig/beni/vendor/checksum.rbs
Overview
SHA256 verification for vendored tarballs. One instance per +(path,
expected_sha)+ pair; reuse is not supported and not needed by
Beni::Tasks. Operates in two modes:
* Explicit expected hash (a built-in pair entry or a consumer
override) — must match exactly; mismatch raises.
* Trust-on-first-use (TOFU) — when +expected_sha+ is +nil+ or empty,
the actual hash is pinned to a +.sha256+ sidecar next to the
tarball. Subsequent runs compare against the pinned value and
raise on drift.
Public contract is the single #verify_or_pin entry point; the two
branches and the digest helper are internal.
Instance Method Summary collapse
- #expected? ⇒ Boolean
-
#initialize(path, expected_sha) ⇒ Checksum
constructor
A new instance of Checksum.
- #sha256 ⇒ String
- #verify_against_expected(actual, sidecar) ⇒ void
-
#verify_or_pin ⇒ String
Verify the tarball against
expected_sha(if non-empty) or TOFU-pin against the.sha256sidecar. - #verify_or_pin_sidecar(actual, sidecar) ⇒ void
Constructor Details
#initialize(path, expected_sha) ⇒ Checksum
Returns a new instance of Checksum.
21 22 23 24 |
# File 'lib/beni/vendor/checksum.rb', line 21 def initialize(path, expected_sha) @path = path @expected_sha = expected_sha end |
Instance Method Details
#expected? ⇒ Boolean
40 41 42 |
# File 'lib/beni/vendor/checksum.rb', line 40 def expected? !@expected_sha.to_s.empty? end |
#sha256 ⇒ String
44 45 46 |
# File 'lib/beni/vendor/checksum.rb', line 44 def sha256 Digest::SHA256.file(@path).hexdigest end |
#verify_against_expected(actual, sidecar) ⇒ void
This method returns an undefined value.
48 49 50 51 52 53 54 |
# File 'lib/beni/vendor/checksum.rb', line 48 def verify_against_expected(actual, sidecar) unless actual == @expected_sha raise Error, "[beni] checksum mismatch for #{File.basename(@path)}: " \ "expected #{@expected_sha}, got #{actual}" end File.write(sidecar, "#{actual}\n") end |
#verify_or_pin ⇒ String
Verify the tarball against expected_sha (if non-empty) or TOFU-pin
against the .sha256 sidecar. Returns the computed SHA256 hex digest
on success. Raises Beni::Error on mismatch (explicit mode) or drift
(TOFU mode); both error messages carry a [beni] prefix for CI log
grepping.
31 32 33 34 35 36 |
# File 'lib/beni/vendor/checksum.rb', line 31 def verify_or_pin actual = sha256 sidecar = "#{@path}.sha256" expected? ? verify_against_expected(actual, sidecar) : verify_or_pin_sidecar(actual, sidecar) actual end |
#verify_or_pin_sidecar(actual, sidecar) ⇒ void
This method returns an undefined value.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/beni/vendor/checksum.rb', line 56 def verify_or_pin_sidecar(actual, sidecar) if File.exist?(sidecar) pinned = File.read(sidecar).strip return if actual == pinned raise Error, "[beni] checksum drift for #{File.basename(@path)}: " \ "pinned #{pinned}, got #{actual}" end File.write(sidecar, "#{actual}\n") end |