mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-06-16 08:25:18 +02:00
Initial Commit 🚀 🚀
This commit is contained in:
commit
4f2a629340
444 changed files with 76863 additions and 0 deletions
31
api/utils/api_key.py
Normal file
31
api/utils/api_key.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import hashlib
|
||||
import secrets
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def generate_api_key() -> Tuple[str, str, str]:
|
||||
"""Generate a new API key with its hash and prefix.
|
||||
|
||||
Returns:
|
||||
Tuple of (raw_api_key, key_hash, key_prefix)
|
||||
- raw_api_key: The actual API key to give to the user
|
||||
- key_hash: SHA256 hash of the key for storage
|
||||
- key_prefix: First 8 characters for display purposes
|
||||
"""
|
||||
raw_api_key = f"dgr_{secrets.token_urlsafe(32)}"
|
||||
key_hash = hashlib.sha256(raw_api_key.encode()).hexdigest()
|
||||
key_prefix = raw_api_key[:8]
|
||||
|
||||
return raw_api_key, key_hash, key_prefix
|
||||
|
||||
|
||||
def hash_api_key(raw_api_key: str) -> str:
|
||||
"""Hash an API key for comparison.
|
||||
|
||||
Args:
|
||||
raw_api_key: The raw API key to hash
|
||||
|
||||
Returns:
|
||||
SHA256 hash of the API key
|
||||
"""
|
||||
return hashlib.sha256(raw_api_key.encode()).hexdigest()
|
||||
Loading…
Add table
Add a link
Reference in a new issue