Skip to content

Instantly share code, notes, and snippets.

@cubapp
Created June 6, 2024 17:24
Show Gist options
  • Save cubapp/303bcf630ada4ddb496bfcf5a5eace3a to your computer and use it in GitHub Desktop.
Save cubapp/303bcf630ada4ddb496bfcf5a5eace3a to your computer and use it in GitHub Desktop.
Benchmarking python script, taken from PHP version via https://3v4l.org/rMd8D
import time
import random
import string
import bcrypt
import hashlib
# Function to generate random string
def get_random_string(chars, length):
return ''.join(random.choice(chars) for _ in range(length))
chars = string.ascii_lowercase + string.digits
lens = [10, 100, 1000, 1024 * 1024, 1024 * 1024 * 10]
# bcrypt
for length in lens:
pw = get_random_string(chars, length).encode('utf-8')
start_time = time.time()
bcrypt.hashpw(pw, bcrypt.gensalt())
elapsed_time = time.time() - start_time
print(f'{elapsed_time:.6f} ms (bcrypt {len(pw)} chars)')
# PBKDF2
for length in lens:
pw = get_random_string(chars, length).encode('utf-8')
start_time = time.time()
hashlib.pbkdf2_hmac('sha256', pw, b'salt', 100000)
elapsed_time = time.time() - start_time
print(f'{elapsed_time:.6f} ms (PBKDF2 {len(pw)} chars)')
# SHA-1
for length in lens:
pw = get_random_string(chars, length).encode('utf-8')
start_time = time.time()
hashlib.sha1(pw).hexdigest()
elapsed_time = time.time() - start_time
print(f'{elapsed_time:.6f} ms (SHA-1 {len(pw)} chars)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment