Coverage for src/kwai/core/domain/value_objects/password.py: 100%
13 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module that defines a value object for a password."""
3from dataclasses import dataclass
4from typing import Self
6import bcrypt
9@dataclass(frozen=True, slots=True)
10class Password:
11 """A value object for a password."""
13 hashed_password: bytes
15 @classmethod
16 def create_from_string(cls, password: str) -> Self:
17 """Create a password from a string."""
18 return cls(bcrypt.hashpw(password.encode(), bcrypt.gensalt()))
20 def verify(self, password: str) -> bool:
21 """Verify this password against the given password."""
22 return bcrypt.checkpw(password.encode(), self.hashed_password)
24 def __str__(self):
25 """Return a string representation (hashed password)."""
26 return self.hashed_password.decode()