Commit b17b0820 by Bartosz Sokorski Committed by GitHub

Update hashlib usage (#8058)

parent 36153326
...@@ -27,8 +27,6 @@ if TYPE_CHECKING: ...@@ -27,8 +27,6 @@ if TYPE_CHECKING:
from poetry.poetry import Poetry from poetry.poetry import Poetry
_has_blake2 = hasattr(hashlib, "blake2b")
class UploadError(Exception): class UploadError(Exception):
def __init__(self, error: ConnectionError | HTTPError | str) -> None: def __init__(self, error: ConnectionError | HTTPError | str) -> None:
...@@ -115,8 +113,7 @@ class Uploader: ...@@ -115,8 +113,7 @@ class Uploader:
file_type = self._get_type(file) file_type = self._get_type(file)
if _has_blake2: blake2_256_hash = hashlib.blake2b(digest_size=256 // 8)
blake2_256_hash = hashlib.blake2b(digest_size=256 // 8)
md5_hash = hashlib.md5() md5_hash = hashlib.md5()
sha256_hash = hashlib.sha256() sha256_hash = hashlib.sha256()
...@@ -124,15 +121,11 @@ class Uploader: ...@@ -124,15 +121,11 @@ class Uploader:
for content in iter(lambda: fp.read(io.DEFAULT_BUFFER_SIZE), b""): for content in iter(lambda: fp.read(io.DEFAULT_BUFFER_SIZE), b""):
md5_hash.update(content) md5_hash.update(content)
sha256_hash.update(content) sha256_hash.update(content)
blake2_256_hash.update(content)
if _has_blake2:
blake2_256_hash.update(content)
md5_digest = md5_hash.hexdigest() md5_digest = md5_hash.hexdigest()
sha2_digest = sha256_hash.hexdigest() sha2_digest = sha256_hash.hexdigest()
blake2_256_digest: str | None = None blake2_256_digest = blake2_256_hash.hexdigest()
if _has_blake2:
blake2_256_digest = blake2_256_hash.hexdigest()
py_version: str | None = None py_version: str | None = None
if file_type == "bdist_wheel": if file_type == "bdist_wheel":
......
...@@ -74,54 +74,44 @@ def test_default_hash(fixture_dir: FixtureDirGetter) -> None: ...@@ -74,54 +74,44 @@ def test_default_hash(fixture_dir: FixtureDirGetter) -> None:
assert get_file_hash(fixture_dir("distributions") / "demo-0.1.0.tar.gz") == sha_256 assert get_file_hash(fixture_dir("distributions") / "demo-0.1.0.tar.gz") == sha_256
try:
from hashlib import algorithms_guaranteed
except ImportError:
algorithms_guaranteed = {"md5", "sha1", "sha224", "sha256", "sha384", "sha512"}
@pytest.mark.parametrize( @pytest.mark.parametrize(
"hash_name,expected", "hash_name,expected",
[ [
(hash_name, value) ("sha224", "d26bd24163fe91c16b4b0162e773514beab77b76114d9faf6a31e350"),
for hash_name, value in [ (
("sha224", "d26bd24163fe91c16b4b0162e773514beab77b76114d9faf6a31e350"), "sha3_512",
( "196f4af9099185054ed72ca1d4c57707da5d724df0af7c3dfcc0fd018b0e0533908e790a291600c7d196fe4411b4f5f6db45213fe6e5cd5512bf18b2e9eff728", # noqa: E501
"sha3_512", ),
"196f4af9099185054ed72ca1d4c57707da5d724df0af7c3dfcc0fd018b0e0533908e790a291600c7d196fe4411b4f5f6db45213fe6e5cd5512bf18b2e9eff728", # noqa: E501 (
), "blake2s",
( "6dd9007d36c106defcf362cc637abeca41e8e93999928c8fcfaba515ed33bc93",
"blake2s", ),
"6dd9007d36c106defcf362cc637abeca41e8e93999928c8fcfaba515ed33bc93", (
), "sha3_384",
( "787264d7885a0c305d2ee4daecfff435d11818399ef96cacef7e7c6bb638ce475f630d39fdd2800ca187dcd0071dc410", # noqa: E501
"sha3_384", ),
"787264d7885a0c305d2ee4daecfff435d11818399ef96cacef7e7c6bb638ce475f630d39fdd2800ca187dcd0071dc410", # noqa: E501 (
), "blake2b",
( "077a34e8252c8f6776bddd0d34f321cc52762cb4c11a1c7aa9b6168023f1722caf53c9f029074a6eb990a8de341d415dd986293bc2a2fccddad428be5605696e", # noqa: E501
"blake2b", ),
"077a34e8252c8f6776bddd0d34f321cc52762cb4c11a1c7aa9b6168023f1722caf53c9f029074a6eb990a8de341d415dd986293bc2a2fccddad428be5605696e", # noqa: E501 (
), "sha256",
( "9fa123ad707a5c6c944743bf3e11a0e80d86cb518d3cf25320866ca3ef43e2ad",
"sha256", ),
"9fa123ad707a5c6c944743bf3e11a0e80d86cb518d3cf25320866ca3ef43e2ad", (
), "sha512",
( "766ecf369b6bdf801f6f7bbfe23923cc9793d633a55619472cd3d5763f9154711fbf57c8b6ca74e4a82fa9bd8380af831e7b8668e68e362669fc60b1d81d79ad", # noqa: E501
"sha512", ),
"766ecf369b6bdf801f6f7bbfe23923cc9793d633a55619472cd3d5763f9154711fbf57c8b6ca74e4a82fa9bd8380af831e7b8668e68e362669fc60b1d81d79ad", # noqa: E501 (
), "sha384",
( "c638f32460f318035e4600284ba64fb531630740aebd33885946e527002d742787ff09eb65fd81bc34ce5ff5ef11cfe8", # noqa: E501
"sha384", ),
"c638f32460f318035e4600284ba64fb531630740aebd33885946e527002d742787ff09eb65fd81bc34ce5ff5ef11cfe8", # noqa: E501 ("sha3_224", "72980fc7bdf8c4d34268dc469442b09e1ccd2a8ff390954fc4d55a5a"),
), ("sha1", "91b585bd38f72d7ceedb07d03f94911b772fdc4c"),
("sha3_224", "72980fc7bdf8c4d34268dc469442b09e1ccd2a8ff390954fc4d55a5a"), (
("sha1", "91b585bd38f72d7ceedb07d03f94911b772fdc4c"), "sha3_256",
( "7da5c08b416e6bcb339d6bedc0fe077c6e69af00607251ef4424c356ea061fcb",
"sha3_256", ),
"7da5c08b416e6bcb339d6bedc0fe077c6e69af00607251ef4424c356ea061fcb",
),
]
if hash_name in algorithms_guaranteed
], ],
) )
def test_guaranteed_hash( def test_guaranteed_hash(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment