perf: faster generate_hash
This commit is contained in:
parent
a42ca7d8c1
commit
fcaa16bb21
1 changed files with 5 additions and 12 deletions
|
|
@ -1018,19 +1018,12 @@ def get_precision(
|
|||
return get_field_precision(get_meta(doctype).get_field(fieldname), doc, currency)
|
||||
|
||||
|
||||
def generate_hash(txt: str | None = None, length: int | None = None) -> str:
|
||||
"""Generates random hash for given text + current timestamp + random string."""
|
||||
import hashlib
|
||||
import time
|
||||
def generate_hash(txt: str | None = None, length: int | None = 56) -> str:
|
||||
"""Generate random hash using best available randomness source."""
|
||||
import math
|
||||
import secrets
|
||||
|
||||
from .utils import random_string
|
||||
|
||||
digest = hashlib.sha224(
|
||||
((txt or "") + repr(time.time()) + repr(random_string(8))).encode()
|
||||
).hexdigest()
|
||||
if length:
|
||||
digest = digest[:length]
|
||||
return digest
|
||||
return secrets.token_hex(math.ceil(length / 2))[:length]
|
||||
|
||||
|
||||
def reset_metadata_version():
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue