From 22685052c512f2d9cc8765e06342a1c98abacaca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Cardao?= Date: Wed, 11 Dec 2024 18:10:36 +0100 Subject: [PATCH] Add sha512 support This is sometimes necessary to check file integrity. i.e: NPM package seems mainly to use sha512 for package integrity Needed for it/package-registry#27 --- src/e3/hash.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/e3/hash.py b/src/e3/hash.py index 2bff96eb..537419a6 100644 --- a/src/e3/hash.py +++ b/src/e3/hash.py @@ -18,7 +18,7 @@ class HashError(e3.error.E3Error): def __compute_hash( path: PathLike[str] | str, - kind: Literal["md5"] | Literal["sha1"] | Literal["sha256"], + kind: Literal["md5"] | Literal["sha1"] | Literal["sha256"] | Literal["sha512"], ) -> str: if not os.path.isfile(path): raise HashError(kind, f"cannot find {path}") @@ -64,3 +64,14 @@ def sha256(path: PathLike[str] | str) -> str: :raise HashError: in case of error """ return __compute_hash(path, "sha256") + + +def sha512(path: PathLike[str] | str) -> str: + """Compute sha512 hexadecimal digest of a file. + + :param str path: path to a file + + :return: the hash of the file content + :raise HashError: in case of error + """ + return __compute_hash(path, "sha512")