import os
import unicodedata
import hashlib
import hmac
# exactly the same params as better-auth
SCRYPT_PARAMS = dict(n=16384, r=16, p=1, dklen=64)
# mirror their maxmem: 128 * N * r * 2
MAXMEM = 128 * SCRYPT_PARAMS["n"] * SCRYPT_PARAMS["r"] * 2
def hash_password(password: str) -> str:
"""
→ salt:key just like better-auth (hex:salt, hex:key)
→ no BETTER_AUTH_SECRET is used here
"""
# 1) Unicode NFKC normalization
normalized = unicodedata.normalize("NFKC", password)
# 2) random 16-byte salt
salt = os.urandom(16)
# 3) scrypt with explicit maxmem
key = hashlib.scrypt(
normalized.encode("utf-8"),
salt=salt,
n=SCRYPT_PARAMS["n"],
r=SCRYPT_PARAMS["r"],
p=SCRYPT_PARAMS["p"],
dklen=SCRYPT_PARAMS["dklen"],
maxmem=MAXMEM,
)
# 4) return hex(salt):hex(key)
return f"{salt.hex()}:{key.hex()}"
pw = "12345678"
h = hash_password(pw)
print("Hash:", h)
import os
import unicodedata
import hashlib
import hmac
# exactly the same params as better-auth
SCRYPT_PARAMS = dict(n=16384, r=16, p=1, dklen=64)
# mirror their maxmem: 128 * N * r * 2
MAXMEM = 128 * SCRYPT_PARAMS["n"] * SCRYPT_PARAMS["r"] * 2
def hash_password(password: str) -> str:
"""
→ salt:key just like better-auth (hex:salt, hex:key)
→ no BETTER_AUTH_SECRET is used here
"""
# 1) Unicode NFKC normalization
normalized = unicodedata.normalize("NFKC", password)
# 2) random 16-byte salt
salt = os.urandom(16)
# 3) scrypt with explicit maxmem
key = hashlib.scrypt(
normalized.encode("utf-8"),
salt=salt,
n=SCRYPT_PARAMS["n"],
r=SCRYPT_PARAMS["r"],
p=SCRYPT_PARAMS["p"],
dklen=SCRYPT_PARAMS["dklen"],
maxmem=MAXMEM,
)
# 4) return hex(salt):hex(key)
return f"{salt.hex()}:{key.hex()}"
pw = "12345678"
h = hash_password(pw)
print("Hash:", h)