Extend password policy and generator

This commit is contained in:
thePR0M3TH3AN committed 2025-07-30 18:44:41 -04:00
1 parent d4d475438f
commit dcb5c6e805
5 files changed
+130 -24

No files matched your search

+2 -2
View File
@@ -39,7 +39,7 @@ def test_zero_policy_preserves_length():
pg = make_generator(policy)
alphabet = string.ascii_lowercase
dk = bytes(range(32))
result = pg._enforce_complexity("a" * 32, alphabet, dk)
result = pg._enforce_complexity("a" * 32, alphabet, "", dk)
assert len(result) == 32
@@ -50,7 +50,7 @@ def test_custom_policy_applied():
pg = make_generator(policy)
alphabet = string.ascii_letters + string.digits + string.punctuation
dk = bytes(range(32))
result = pg._enforce_complexity("a" * 32, alphabet, dk)
result = pg._enforce_complexity("a" * 32, alphabet, string.punctuation, dk)
counts = count_types(result)
assert counts[0] >= 4
assert counts[1] >= 1
+1 -1
View File
@@ -41,7 +41,7 @@ def test_enforce_complexity_minimum_counts():
pg = make_generator()
alphabet = string.ascii_letters + string.digits + string.punctuation
dk = bytes(range(32))
result = pg._enforce_complexity("a" * 32, alphabet, dk)
result = pg._enforce_complexity("a" * 32, alphabet, string.punctuation, dk)
assert sum(1 for c in result if c.isupper()) >= 2
assert sum(1 for c in result if c.islower()) >= 2
assert sum(1 for c in result if c.isdigit()) >= 2
+49
View File
@@ -0,0 +1,49 @@
import string
from pathlib import Path
import sys
sys.path.append(str(Path(__file__).resolve().parents[1]))
from seedpass.core.password_generation import PasswordGenerator, PasswordPolicy
class DummyEnc:
def derive_seed_from_mnemonic(self, mnemonic):
return b"\x00" * 32
class DummyBIP85:
def derive_entropy(self, index: int, bytes_len: int, app_no: int = 32) -> bytes:
return bytes((index + i) % 256 for i in range(bytes_len))
def make_generator(policy=None):
pg = PasswordGenerator.__new__(PasswordGenerator)
pg.encryption_manager = DummyEnc()
pg.bip85 = DummyBIP85()
pg.policy = policy or PasswordPolicy()
return pg
def test_no_special_chars():
policy = PasswordPolicy(include_special_chars=False)
pg = make_generator(policy)
pw = pg.generate_password(length=16, index=0)
assert not any(c in string.punctuation for c in pw)
def test_allowed_special_chars_only():
allowed = "@$"
policy = PasswordPolicy(allowed_special_chars=allowed)
pg = make_generator(policy)
pw = pg.generate_password(length=32, index=1)
specials = [c for c in pw if c in string.punctuation]
assert specials and all(c in allowed for c in specials)
def test_exclude_ambiguous_chars():
policy = PasswordPolicy(exclude_ambiguous=True)
pg = make_generator(policy)
pw = pg.generate_password(length=32, index=2)
for ch in "O0Il1":
assert ch not in pw