@@ -34,7 +34,7 @@ _revisions_cache = bb.checksum.RevisionsCache()
logger = logging.getLogger("BitBake.Fetcher")
-CHECKSUM_LIST = [ "md5", "sha256", "sha1", "sha384", "sha512" ]
+CHECKSUM_LIST = [ "h1", "md5", "sha256", "sha1", "sha384", "sha512" ]
SHOWN_CHECKSUM_LIST = ["sha256"]
class BBFetchException(Exception):
@@ -585,6 +585,31 @@ def sha512_file(filename):
import hashlib
return _hasher(hashlib.sha512(), filename)
+def h1_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"""