Skip to content

Commit 709127a

Browse files
authored
Add support for SHA512 (#1)
1 parent cc45596 commit 709127a

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Tested on:
1515

1616
## Documentation
1717

18-
Latest version is 1.0.1
18+
Latest version is 1.0.2
1919

2020
## Installation
2121

@@ -31,6 +31,7 @@ The supported checksum algorithms are:
3131

3232
- md5
3333
- sha256
34+
- sha512
3435

3536
#### Target files sizes
3637

pyntegrity/config.py

+3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
"""
1818
# Hash settings
1919

20+
SHA512_LENGTH = 128
2021
SHA256_LENGTH = 64
2122
MD5_LENGTH = 32
2223

24+
SHA512_REX = r"^[A-Fa-f\d]{128}$"
2325
SHA256_REX = r"^[A-Fa-f\d]{64}$"
2426
MD5_REX = r"^[a-fA-F\d]{32}$"
2527

2628
SUPPORTED_HASH_ALGOS = {
2729
"sha256": {"REX": SHA256_REX, "LENGTH": SHA256_LENGTH},
2830
"md5": {"REX": MD5_REX, "LENGTH": MD5_LENGTH},
31+
"sha512": {"REX": SHA512_REX, "LENGTH": SHA512_LENGTH},
2932
}

pyntegrity/core.py

-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929

3030
from .config import SUPPORTED_HASH_ALGOS
3131

32-
HashAlgoEnum = enum.Enum(
33-
"HashAlgoEnum", {key: key for key in SUPPORTED_HASH_ALGOS.keys()}
34-
)
35-
3632

3733
def detect_hash_algo(checksum_str: str):
3834
"""

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="pyntegrity",
8-
version="1.0.1",
8+
version="1.0.2",
99
description="Pyntegrity is a Python package that helps you check a file integrity.",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",

tests/test_core.py

+61-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ def test_validate_checksum_str_sha256_nok(self):
5050
with self.assertRaises(HashStrNotValidException):
5151
validate_checksum_str(invalid_sha256, "sha256")
5252

53+
def test_validate_checksum_str_sha512_ok(self):
54+
valid_sha512 = "d80f94038e28749518984f0a7827530fc272117f952db85d54baa05ea89b6fc6fc9a4059be18c9b8762139404bd43c59e7ad5814ddc0eb73bdc6a6355b16b718"
55+
self.assertTrue(validate_checksum_str(valid_sha512, "sha512"))
56+
57+
def test_validate_checksum_str_sha512_nok(self):
58+
invalid_sha512 = "d80f94038e28749518984f0a78275x0fc272117f952db85d54baa05ea89b6fc6fc9a4059be18c9b8762139404bd43c59e7ad5814ddc0eb73bdc6a6355b16b718"
59+
with self.assertRaises(HashStrNotValidException):
60+
validate_checksum_str(invalid_sha512, "sha512")
61+
5362

5463
class TestGetFileFromStr(TestCase):
5564
def test_get_file_path_from_str_ok(self):
@@ -66,7 +75,7 @@ def test_get_file_path_from_str_nok_not_found(self):
6675

6776

6877
class TestGetFileChecksum(TestCase):
69-
def test_get_file_checksum_text(self):
78+
def test_get_file_checksum_text_md5_true(self):
7079
obj = IntegrityValidator(
7180
str_path="tests/data/test_file.txt",
7281
checksum_str="bbe4f28b27120e0a9611d90d242bc656",
@@ -76,7 +85,7 @@ def test_get_file_checksum_text(self):
7685
"bbe4f28b27120e0a9611d90d242bc656",
7786
)
7887

79-
def test_get_file_checksum_bin(self):
88+
def test_get_file_checksum_bin_md5_true(self):
8089
obj = IntegrityValidator(
8190
str_path="tests/data/test_file.dat",
8291
checksum_str="0cb988d042a7f28dd5fe2b55b3f5ac7a",
@@ -86,6 +95,56 @@ def test_get_file_checksum_bin(self):
8695
"0cb988d042a7f28dd5fe2b55b3f5ac7a",
8796
)
8897

98+
def test_get_file_checksum_text_md5_false(self):
99+
obj = IntegrityValidator(
100+
str_path="tests/data/test_file.txt",
101+
checksum_str="bbe4f28b27120e0a9611d90d242bc657",
102+
)
103+
self.assertNotEqual(
104+
obj.get_file_checksum("md5"),
105+
"bbe4f28b27120e0a9611d90d242bc657",
106+
)
107+
108+
def test_get_file_checksum_bin_sha256_true(self):
109+
obj = IntegrityValidator(
110+
str_path="tests/data/test_file.dat",
111+
checksum_str="a12871fee210fb8619291eaea194581cbd2531e4b23759d225f6806923f63222",
112+
)
113+
self.assertEqual(
114+
obj.get_file_checksum("sha256"),
115+
"a12871fee210fb8619291eaea194581cbd2531e4b23759d225f6806923f63222",
116+
)
117+
118+
def test_get_file_checksum_text_sha256_false(self):
119+
obj = IntegrityValidator(
120+
str_path="tests/data/test_file.txt",
121+
checksum_str="a12871fee210fb8619291eaea194581cbd2531e4b23759d225f6806923f63223",
122+
)
123+
self.assertNotEqual(
124+
obj.get_file_checksum("sha256"),
125+
"a12871fee210fb8619291eaea194581cbd2531e4b23759d225f6806923f63223",
126+
)
127+
128+
def test_get_file_checksum_bin_sha512_true(self):
129+
obj = IntegrityValidator(
130+
str_path="tests/data/test_file.dat",
131+
checksum_str="bdd81ab233bceb6ad233cd1871509320a18d0335a891cf98730990e8923e1dda04f3358e9c7e1c3d16b16f408cfafb6af53254ef3023ed2436533808b6ca9933",
132+
)
133+
self.assertEqual(
134+
obj.get_file_checksum("sha512"),
135+
"bdd81ab233bceb6ad233cd1871509320a18d0335a891cf98730990e8923e1dda04f3358e9c7e1c3d16b16f408cfafb6af53254ef3023ed2436533808b6ca9933",
136+
)
137+
138+
def test_get_file_checksum_text_sha512_false(self):
139+
obj = IntegrityValidator(
140+
str_path="tests/data/test_file.txt",
141+
checksum_str="add81ab233bceb6ad233cd1871509320a18d0335a891cf98730990e8923e1dda04f3358e9c7e1c3d16b16f408cfafb6af53254ef3023ed2436533808b6ca9933",
142+
)
143+
self.assertNotEqual(
144+
obj.get_file_checksum("sha512"),
145+
"add81ab233bceb6ad233cd1871509320a18d0335a891cf98730990e8923e1dda04f3358e9c7e1c3d16b16f408cfafb6af53254ef3023ed2436533808b6ca9933",
146+
)
147+
89148

90149
class TestValidateFileIntegrity(TestCase):
91150
def test_validate_file_integrity_true(self):

0 commit comments

Comments
 (0)