esphome/esphome/dashboard/util.py

33 lines
783 B
Python
Raw Normal View History

import hashlib
2023-01-16 22:28:09 +01:00
import unicodedata
from esphome.const import ALLOWED_NAME_CHARS
def password_hash(password: str) -> bytes:
"""Create a hash of a password to transform it to a fixed-length digest.
Note this is not meant for secure storage, but for securely comparing passwords.
"""
return hashlib.sha256(password.encode()).digest()
2023-01-16 22:28:09 +01:00
def strip_accents(value):
return "".join(
c
for c in unicodedata.normalize("NFD", str(value))
if unicodedata.category(c) != "Mn"
)
def friendly_name_slugify(value):
value = (
strip_accents(value)
.lower()
.replace(" ", "-")
.replace("_", "-")
.replace("--", "-")
.strip("-")
)
return "".join(c for c in value if c in ALLOWED_NAME_CHARS)