2 This module provides utilities related to hashing files
10 This exception is raised whenever provided algorithm is not one of allowed algorithms.
18 This exception is raised when hashing was requested on a FileHasher that has already finished hashing.
26 This exception is raised when a hash was requested from a FileHasher that has not hashed any file yet.
34 This class provides hashlib-based file hasher
38 hashlib_instance =
None
50 Currently supported hashes: SHA1, SHA256, SHA384, SHA512, MD5 (not recommended)
52 @param algo String with name of algorithm to use
54 @throws UnsupportedAlgorithmException when attempting to use unsupported algorithm
56 if algo
is None or algo.lower() ==
"" or algo.lower() ==
"sha1":
58 elif algo.lower() ==
"sha256":
60 elif algo.lower() ==
"sha384":
62 elif algo.lower() ==
"sha512":
64 elif algo.lower() ==
"md5":
71 Hashes file-object with currently chosen algorithm
73 @param file file-object to be hashed
75 @throws AlreadyDoneException if this FileHasher is already done hashing a different file
79 b = bytearray(128 * 1024)
81 for n
in iter(
lambda: file.readinto(mv), 0):
89 Hashes file-object with currently chosen algorithm
91 @param path path to file to be hashed
93 @throws AlreadyDoneException if this FileHasher is already done hashing a different file
97 b = bytearray(128 * 1024)
99 with open(path,
"rb", buffering=0)
as f:
100 for n
in iter(
lambda: f.readinto(mv), 0):
108 Returns hexadecimal value of hash from this object.
110 @returns hexadecimal string representation of hash
112 @throws NotDoneYetException if no file was hashed with this FileHasher.
121 Returns hexadecimal value of hash from this object.
123 @returns bytes of hash
125 @throws NotDoneYetException if no file was hashed with this FileHasher.
134 Hashes a file-object and returns hash
136 @param file file-object to be hashed
138 @returns bytes of hash
140 @throws AlreadyDoneException if this FileHasher is already done hashing a different file
147 Hashes a file at path and returns hash
149 @param path path to the file to be hashed
151 @returns bytes of hash
153 @throws AlreadyDoneException if this FileHasher is already done hashing a different file
160 Hashes a file-object and returns hash
162 @param file file-object to be hashed
164 @returns hexadecimal string representation of hash
166 @throws AlreadyDoneException if this FileHasher is already done hashing a different file
173 Hashes a file at path and returns hash
175 @param path path to the file to be hashed
177 @returns hexadecimal string representation of hash
179 @throws AlreadyDoneException if this FileHasher is already done hashing a different file
This exception is raised when hashing was requested on a FileHasher that has already finished hashing...
This class provides hashlib-based file hasher.
def hash_file_and_get_hashbytes(self, file)
Hashes a file-object and returns hash.
def hash_path_and_get_hashbytes(self, path)
Hashes a file at path and returns hash.
def hash_file_and_get_hexdigest(self, file)
Hashes a file-object and returns hash.
def hash_path_and_get_hexdigest(self, path)
Hashes a file at path and returns hash.
def get_hashbytes(self)
Returns hexadecimal value of hash from this object.
hexdigest
result of hashing (hex)
def hash_file(self, file)
Hashes file-object with currently chosen algorithm.
def get_hexdigest(self)
Returns hexadecimal value of hash from this object.
def __init__(self, algo=None)
Initializes hasher.
def hash_path(self, path)
Hashes file-object with currently chosen algorithm.
bool islocked
Is this FileHasher done hashing a file.
digest
result of hashing (raw)
hashlib_instance
Instance of hashlib algorithm implementation.
This exception is raised when a hash was requested from a FileHasher that has not hashed any file yet...
This exception is raised whenever provided algorithm is not one of allowed algorithms.