Skip to content

Instantly share code, notes, and snippets.

@bmbouter
Created September 5, 2024 08:04
Show Gist options
  • Select an option

  • Save bmbouter/bb99b3b66899ae3bdff9df6ca1fdfe4b to your computer and use it in GitHub Desktop.

Select an option

Save bmbouter/bb99b3b66899ae3bdff9df6ca1fdfe4b to your computer and use it in GitHub Desktop.
A script to measure the `update()` and `hexdigest()` calls when computing multiple hashers
import hashlib
import os
import time
# Function to generate a large binary data set (e.g., 100 MB)
def generate_large_binary_data(size_in_mb):
# Generate random binary data of the specified size
return os.urandom(size_in_mb * 1024 * 1024) # Convert MB to bytes
# Function to compute multiple hashes from the same data input and measure time per hasher
def compute_hashes(data):
# Initialize hash objects for SHA-224, SHA-256, SHA-384, and SHA-512
hashers = {
"SHA-224": hashlib.sha224(),
"SHA-256": hashlib.sha256(),
"SHA-384": hashlib.sha384(),
"SHA-512": hashlib.sha512()
}
# Store timing results
timings = {}
# Measure the time for the update() call per hasher
for name, hasher in hashers.items():
start_update_time = time.time()
hasher.update(data)
end_update_time = time.time()
update_duration = end_update_time - start_update_time
timings[name] = {'update': update_duration}
# Measure the time for the hexdigest() call per hasher
for name, hasher in hashers.items():
start_hexdigest_time = time.time()
_ = hasher.hexdigest()
end_hexdigest_time = time.time()
hexdigest_duration = end_hexdigest_time - start_hexdigest_time
timings[name]['hexdigest'] = hexdigest_duration
return timings
def print_results_table(timings):
# Print results in a table format without using any external library
print("+------------+-------------------+-------------------+")
print("| Hash Type | update() Time (s) | hexdigest() Time (s) |")
print("+------------+-------------------+-------------------+")
for name, timing in timings.items():
print(f"| {name:<10} | {timing['update']:.6f} | {timing['hexdigest']:.6f} |")
print("+------------+-------------------+-------------------+")
def main():
# Generate large binary data of 100 MB
binary_data = generate_large_binary_data(10000)
# Compute hashes and measure time per hasher
timings = compute_hashes(binary_data)
# Print the timing results in a table format
print_results_table(timings)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment