Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user specified blocksize and increase default to 8192 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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