From f00e8e1233e773d495413ea31000b88f67eb61ba Mon Sep 17 00:00:00 2001 From: Stefan Herbrechtsmeier Date: Tue, 7 Jan 2025 16:15:13 +0100 Subject: bitbake: utils: add Go mod h1 checksum support Add support for the Go mod h1 hash. The hash is based on the Go dirhash package. The package defines hashes over directory trees and is uses for Go mod files and zip archives. (Bitbake rev: deefb01592f717efba68e3997fefd04dc7611d88) Signed-off-by: Stefan Herbrechtsmeier Signed-off-by: Richard Purdie --- bitbake/lib/bb/utils.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'bitbake/lib/bb/utils.py') diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index e722f9113d..135c71c007 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py @@ -585,6 +585,31 @@ def sha512_file(filename): import hashlib return _hasher(hashlib.sha512(), filename) +def goh1_file(filename): + """ + Return the hex string representation of the Go mod h1 checksum of the + filename. The Go mod h1 checksum uses the Go dirhash package. The package + defines hashes over directory trees and is used by go mod for mod files and + zip archives. + """ + import hashlib + import zipfile + + lines = [] + if zipfile.is_zipfile(filename): + with zipfile.ZipFile(filename) as archive: + for fn in sorted(archive.namelist()): + method = hashlib.sha256() + method.update(archive.read(fn)) + hash = method.hexdigest() + lines.append("%s %s\n" % (hash, fn)) + else: + hash = _hasher(hashlib.sha256(), filename) + lines.append("%s go.mod\n" % hash) + method = hashlib.sha256() + method.update("".join(lines).encode('utf-8')) + return method.hexdigest() + def preserved_envvars_exported(): """Variables which are taken from the environment and placed in and exported from the metadata""" -- cgit v1.2.3-54-g00ecf