Spaces:
Runtime error
Runtime error
File size: 878 Bytes
880d4f5 4729829 880d4f5 4729829 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import secrets
import json
import os
API_KEYS_FILE = 'api_keys.json'
def generate_api_key():
"""Generate a secure random API key."""
return secrets.token_hex(32)
def save_api_key(user_id, api_key):
"""Save the API key associated with a user."""
if os.path.exists(API_KEYS_FILE):
with open(API_KEYS_FILE, 'r') as f:
api_keys = json.load(f)
else:
api_keys = {}
api_keys[user_id] = api_key
with open(API_KEYS_FILE, 'w') as f:
json.dump(api_keys, f)
def generate_and_save_api_keys(num_keys=15):
"""Generate and save a specified number of API keys."""
for i in range(1, num_keys + 1):
user_id = f"user_{i}"
api_key = generate_api_key()
save_api_key(user_id, api_key)
print(f"API Key for {user_id}: {api_key}")
if __name__ == "__main__":
generate_and_save_api_keys(15)
|