Skip to content

Instantly share code, notes, and snippets.

@ourway
Created March 12, 2023 09:40
Show Gist options
  • Save ourway/4fb428c27522ee9cf400422e5c83913d to your computer and use it in GitHub Desktop.
Save ourway/4fb428c27522ee9cf400422e5c83913d to your computer and use it in GitHub Desktop.
Find the shard_number based on the key
import hashlib
# Get user input
key = input("Enter key: ")
num_shards = int(input("Enter number of shard databases: "))
# Hash the key
hashed_key = hashlib.sha256(key.encode()).hexdigest()
# Convert the hash to an integer
hashed_int = int(hashed_key, 16)
# Determine the shard number
shard_number = hashed_int % num_shards
# Print the shard number and the hash key
print(f"The key '{key}' with hash '{hashed_key}' goes to shard {shard_number}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment