Skip to content

Commit

Permalink
Add support for user specified blocksize and increase default to 8192
Browse files Browse the repository at this point in the history
  • Loading branch information
djhejna committed Dec 6, 2020
1 parent 01a79af commit 5bdc0df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion filehash/filehash.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class FileHash:
Class wrapping the hashlib module to facilitate calculating file hashes.
"""

def __init__(self, hash_algorithm='sha256', chunk_size=4096):
def __init__(self, hash_algorithm='sha256', chunk_size=8192):
"""
Initialize the FileHash class.
Expand All @@ -164,6 +164,7 @@ def __init__(self, hash_algorithm='sha256', chunk_size=4096):
if hash_algorithm not in SUPPORTED_ALGORITHMS:
raise ValueError("Error, unsupported hash/checksum algorithm: {0}".format(hash_algorithm))
self.chunk_size = chunk_size
# print("FileHash:INIT: chunk_size =", self.chunk_size)
self.hash_algorithm = hash_algorithm

def hash_file(self, filename):
Expand Down
13 changes: 11 additions & 2 deletions filehash/filehash_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


default_hash_algorithm = 'sha256'

default_chunk_size = 8192
def create_parser():
parser = argparse.ArgumentParser(
description="Tool for calculating the checksum / hash of a file or directory."
Expand All @@ -20,6 +20,15 @@ def create_parser():
),
default=default_hash_algorithm
)
parser.add_argument(
u"-b",
u"--blocksize",
type=int,
help=u"Blocksize/ChunkSize for hash algorithm to use. Valid values are multiples of 4096. Defaults to \"{0}\"".format(
default_chunk_size
),
default=default_chunk_size
)

parser_group = parser.add_mutually_exclusive_group(required=True)
parser_group.add_argument(
Expand Down Expand Up @@ -95,7 +104,7 @@ def main():
parser.print_help()
sys.exit(1)

hasher = FileHash(args.algorithm.lower())
hasher = FileHash(args.algorithm.lower(), args.blocksize)
if args.checksums:
process_checksum_file(args.checksums, hasher)
elif args.directory:
Expand Down

0 comments on commit 5bdc0df

Please sign in to comment.