Updated security.
This commit is contained in:
parent
6312dfae47
commit
c709ee11c9
@ -1,9 +1,55 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
|
import uuid
|
||||||
|
|
||||||
DEFAULT_SALT = b"snekker-de-snek-"
|
DEFAULT_SALT = "snekker-de-snek-"
|
||||||
|
DEFAULT_NS = "snekker-de-snek-"
|
||||||
|
|
||||||
|
|
||||||
async def hash(data, salt=DEFAULT_SALT):
|
class UIDNS:
|
||||||
|
def __init__(self, name: str) -> None:
|
||||||
|
"""Initialize UIDNS with a name."""
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def bytes(self) -> bytes:
|
||||||
|
"""Return the bytes representation of the name."""
|
||||||
|
return self.name.encode()
|
||||||
|
|
||||||
|
|
||||||
|
def uid(value: str = None, ns: str = DEFAULT_NS) -> str:
|
||||||
|
"""Generate a UUID based on the provided value and namespace.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value (str): The value to generate the UUID from. If None, a new UUID is created.
|
||||||
|
ns (str): The namespace to use for UUID generation.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The generated UUID as a string.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
ns = ns.decode()
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
if not value:
|
||||||
|
value = str(uuid.uuid4())
|
||||||
|
try:
|
||||||
|
value = value.decode()
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return str(uuid.uuid5(UIDNS(ns), value))
|
||||||
|
|
||||||
|
|
||||||
|
async def hash(data: str, salt: str = DEFAULT_SALT) -> str:
|
||||||
|
"""Hash the given data with the specified salt using SHA-256.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data (str): The data to hash.
|
||||||
|
salt (str): The salt to use for hashing.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The hexadecimal representation of the hashed data.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
data = data.encode(errors="ignore")
|
data = data.encode(errors="ignore")
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@ -18,5 +64,14 @@ async def hash(data, salt=DEFAULT_SALT):
|
|||||||
return obj.hexdigest()
|
return obj.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
async def verify(string: str, hashed: str):
|
async def verify(string: str, hashed: str) -> bool:
|
||||||
|
"""Verify if the given string matches the hashed value.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
string (str): The string to verify.
|
||||||
|
hashed (str): The hashed value to compare against.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the string matches the hashed value, False otherwise.
|
||||||
|
"""
|
||||||
return await hash(string) == hashed
|
return await hash(string) == hashed
|
||||||
|
Loading…
Reference in New Issue
Block a user