Skip to content

Commit

Permalink
Add support for blake2b, blake2s.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidessaguisagjr committed Oct 11, 2021
1 parent 4d6b8ae commit 281bfaa
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
.. image:: https://img.shields.io/travis/leonidessaguisagjr/filehash.svg
:target: https://travis-ci.org/leonidessaguisagjr/filehash

Python module to facilitate calculating the checksum or hash of a file. Tested against Python 2.7.x, Python 3.6.x, Python 3.7.x, Python 3.8.x, Python 3.9.x, Python 3.10.x, PyPy 2.7.x and PyPy3 3.7.x. Currently supports `Adler-32 <https://en.wikipedia.org/wiki/Adler-32>`_, `CRC32 <https://en.wikipedia.org/wiki/Cyclic_redundancy_check>`_, `MD5 <https://en.wikipedia.org/wiki/MD5>`_, `SHA-1 <https://en.wikipedia.org/wiki/SHA-1>`_, `SHA-256 and SHA-512 <https://en.wikipedia.org/wiki/SHA-2>`_.
Python module to facilitate calculating the checksum or hash of a file. Tested against Python 2.7.x, Python 3.6.x, Python 3.7.x, Python 3.8.x, Python 3.9.x, Python 3.10.x, PyPy 2.7.x and PyPy3 3.7.x. Currently supports `Adler-32 <https://en.wikipedia.org/wiki/Adler-32>`_, `BLAKE2b, BLAKE2s <https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2>`_, `CRC32 <https://en.wikipedia.org/wiki/Cyclic_redundancy_check>`_, `MD5 <https://en.wikipedia.org/wiki/MD5>`_, `SHA-1 <https://en.wikipedia.org/wiki/SHA-1>`_, `SHA-224, SHA-256, SHA-384 and SHA-512 <https://en.wikipedia.org/wiki/SHA-2>`_.

(Note: BLAKE2b and BLAKE2s are only supported on Python 3.6.x and later.)

``FileHash`` class
------------------
Expand Down
3 changes: 3 additions & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tox>=3.24.4
twine>=3.4.2
wheel>=0.37.0
6 changes: 6 additions & 0 deletions filehash/filehash.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hashlib
import os
import os.path
import sys
import zlib


Expand Down Expand Up @@ -314,5 +315,10 @@ def verify_sfv(self, sfv_filename):
'sha384' : hashlib.sha384,
'sha512' : hashlib.sha512,
}
# Algorithms that were added in Python 3.6.x
# https://docs.python.org/3.6/library/hashlib.html#hash-algorithms
if sys.version_info.major >= 3 and sys.version_info.minor >= 6:
_ALGORITHM_MAP["blake2b"] = hashlib.blake2b
_ALGORITHM_MAP["blake2s"] = hashlib.blake2s

SUPPORTED_ALGORITHMS = set(_ALGORITHM_MAP.keys())
2 changes: 1 addition & 1 deletion filehash/filehash_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create_parser():
u"-a",
u"--algorithm",
help=u"Checksum/hash algorithm to use. Valid values are: {0}. Defaults to \"{1}\"".format(
", ".join(['"' + a + '"' for a in SUPPORTED_ALGORITHMS]),
", ".join(['"' + a + '"' for a in sorted(SUPPORTED_ALGORITHMS)]),
default_hash_algorithm
),
default=default_hash_algorithm
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
long_description = f.read()

setup(name='filehash',
version='0.1.dev7',
version='0.2.dev1',
description='Module and command-line tool that wraps around hashlib and zlib to facilitate generating checksums / hashes of files and directories.',
long_description=long_description,
long_description_content_type='text/x-rst',
Expand Down
8 changes: 8 additions & 0 deletions test_filehash.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def setUp(self):
self.expected_results = {
'lorem_ipsum.txt': {
'adler32': 'E5ED731F',
'blake2b': 'e9df9d70cfcefb0e2a1e8775ff7b9c0e5f008a76a57310e76a0e43b8fa6801d4eb9f93f1149f155cb85c6e067d99a5c0120205fb52144a29e775da6d4424fc29',
'blake2s': '9725b3b049743de49625880c5a7693a6be7f8c1bc556cbab8ebcf93ce57a15f6',
'crc32': 'A8504B9F',
'md5': '72f5d9e3a5fa2f2e591487ae02489388',
'sha1': 'f7ef3b7afaf1518032da1b832436ef3bbfd4e6f0',
Expand All @@ -26,6 +28,8 @@ def setUp(self):
},
'lorem_ipsum.zip': {
'adler32': '5195A9D6',
'blake2b': 'c37b8c68814d9e04cd260d04cf66e847564b55a4aa99f073ca6030db1559897da252c4cb5c9987c0df05b467e8e502e4034741073bd53d485192c4c6cc1f6481',
'blake2s': '77b1d3377299ef72728928e3837e75bd985ae5ea14f1fa1e5114cf685a392ac9',
'crc32': '7425D3BE',
'md5': '860f55178330e675fb0d55ac1f2c27b2',
'sha1': '03da86258449317e8834a54cf8c4d5b41e7c7128',
Expand All @@ -36,6 +40,8 @@ def setUp(self):
},
'lorem_ipsum_txt+zip.cat': {
'adler32': '8BA81D03',
'blake2b': 'f142f06a6b56d1857a253cb1913885310a3f0d53f5adfd9c1faa154a6866cc739d012a4c21f7dd80b145f1fa9f3c31e615f39890a0a62c7436934d99c8331b03',
'blake2s': 'ad642e5d6dee120f57baa1e8c8dc46631ea773b13fb0d3696e0614603d32794a',
'crc32': 'C2D8AD7F',
'md5': '96a7ef7737b1469621832ef6f5b0bc25',
'sha1': '1ac64d235601ba35d44c56953f338cba294bff9f',
Expand All @@ -46,6 +52,8 @@ def setUp(self):
},
'lorem_ipsum_zip+txt.cat': {
'adler32': 'F0A31D03',
'blake2b': 'c6dd7711e3790daabc5188dc397c290c9811d200cc1efc5f91ea5ff299d7c3dce43040fd183b06ec95dc39d7fd752f96848ac3b3b723f140dfe1c44223dd431f',
'blake2s': 'c0c97777881c047b2db4f4d4433746a5a4eebb15b2ea0a1f2f4ab0a78bb60cff',
'crc32': '6EA6DE9B',
'md5': '5ff44b587e9630bff7134b7e00726b44',
'sha1': 'f1741c227c170061863370cc89af4932fad5fcb7',
Expand Down
4 changes: 4 additions & 0 deletions testdata/hashes.blake2b
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
e9df9d70cfcefb0e2a1e8775ff7b9c0e5f008a76a57310e76a0e43b8fa6801d4eb9f93f1149f155cb85c6e067d99a5c0120205fb52144a29e775da6d4424fc29 *lorem_ipsum.txt
c37b8c68814d9e04cd260d04cf66e847564b55a4aa99f073ca6030db1559897da252c4cb5c9987c0df05b467e8e502e4034741073bd53d485192c4c6cc1f6481 *lorem_ipsum.zip
f142f06a6b56d1857a253cb1913885310a3f0d53f5adfd9c1faa154a6866cc739d012a4c21f7dd80b145f1fa9f3c31e615f39890a0a62c7436934d99c8331b03 *lorem_ipsum_txt+zip.cat
c6dd7711e3790daabc5188dc397c290c9811d200cc1efc5f91ea5ff299d7c3dce43040fd183b06ec95dc39d7fd752f96848ac3b3b723f140dfe1c44223dd431f *lorem_ipsum_zip+txt.cat
4 changes: 4 additions & 0 deletions testdata/hashes.blake2s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
9725b3b049743de49625880c5a7693a6be7f8c1bc556cbab8ebcf93ce57a15f6 *lorem_ipsum.txt
77b1d3377299ef72728928e3837e75bd985ae5ea14f1fa1e5114cf685a392ac9 *lorem_ipsum.zip
ad642e5d6dee120f57baa1e8c8dc46631ea773b13fb0d3696e0614603d32794a *lorem_ipsum_txt+zip.cat
c0c97777881c047b2db4f4d4433746a5a4eebb15b2ea0a1f2f4ab0a78bb60cff *lorem_ipsum_zip+txt.cat

0 comments on commit 281bfaa

Please sign in to comment.