import hashlib
import time
def hash_data_with_retry(data, algorithm='sha256', retry_interval=5, max_retries=3):
"""Hashes user data with retry mechanism."""
for attempt in range(max_retries):
try:
# Hash the data using the specified algorithm
hash_object = hashlib.new(algorithm)
hash_object.update(str(data).encode('utf-8')) # Convert data to string and encode
hex_dig = hash_object.hexdigest()
return hex_dig # Return the hash if successful
except Exception as e:
print(f"Hash error on attempt {attempt + 1}: {e}")
if attempt < max_retries - 1:
time.sleep(retry_interval) # Wait before retrying
else:
print("Max retries reached. Returning None.")
return None # Return None if all retries fail
if __name__ == '__main__':
# Example usage
user_data = "some user data"
hashed_value = hash_data_with_retry(user_data)
if hashed_value:
print(f"Hashed value: {hashed_value}")
else:
print("Hashing failed after multiple retries.")
Add your comment