passphera-core 0.8.2__py3-none-any.whl → 0.9.1__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,2 @@
1
- from passphera_core.generator import PasswordGenerator
1
+ from passphera_core.entities.generator import Generator
2
2
  from passphera_core.exceptions import InvalidAlgorithmException
File without changes
@@ -0,0 +1,78 @@
1
+ from datetime import datetime, timezone
2
+ from uuid import UUID
3
+
4
+ from passphera_core.entities import Generator, GeneratorConfig
5
+ from passphera_core.interfaces import GeneratorRepository, GeneratorConfigRepository
6
+
7
+
8
+ class GetGeneratorUseCase:
9
+ def __init__(self, generator_repository: GeneratorRepository):
10
+ self.generator_repository: GeneratorRepository = generator_repository
11
+
12
+ def execute(self, user_id: UUID) -> Generator:
13
+ return self.generator_repository.find_by_user_id(user_id)
14
+
15
+
16
+ class GetGeneratorConfigPropertyUseCase:
17
+ def __init__(self, generator_repository: GeneratorRepository, generator_config_repository: GeneratorConfigRepository):
18
+ self.generator_repository: GeneratorRepository = generator_repository
19
+ self.generator_config_repository: GeneratorConfigRepository = generator_config_repository
20
+
21
+ def execute(self, user_id: UUID, field: str) -> str:
22
+ generator_entity: Generator = self.generator_repository.find_by_user_id(user_id)
23
+ generator_config_entity: GeneratorConfig = self.generator_config_repository.find_by_generator_id(generator_entity.id)
24
+ return getattr(generator_config_entity, field)
25
+
26
+
27
+ class UpdateGeneratorConfigUseCase:
28
+ def __init__(
29
+ self,
30
+ generator_repository: GeneratorRepository,
31
+ generator_config_repository: GeneratorConfigRepository,
32
+ ):
33
+ self.generator_repository: GeneratorRepository = generator_repository
34
+ self.generator_config_repository: GeneratorConfigRepository = generator_config_repository
35
+
36
+ def execute(self, user_id: UUID, field: str, value: str) -> None:
37
+ generator_entity: Generator = self.generator_repository.find_by_user_id(user_id)
38
+ generator_config_entity: GeneratorConfig = self.generator_config_repository.find_by_generator_id(generator_entity.id)
39
+ setattr(generator_config_entity, field, value)
40
+ if field == 'algorithm':
41
+ generator_config_entity.get_algorithm()
42
+ generator_config_entity.updated_at = datetime.now(timezone.utc)
43
+ self.generator_config_repository.update(generator_config_entity)
44
+
45
+
46
+
47
+ class AddCharacterReplacementUseCase:
48
+ def __init__(
49
+ self,
50
+ generator_repository: GeneratorRepository,
51
+ generator_config_repository: GeneratorConfigRepository,
52
+ ):
53
+ self.generator_repository: GeneratorRepository = generator_repository
54
+ self.generator_config_repository: GeneratorConfigRepository = generator_config_repository
55
+
56
+ def execute(self, user_id: UUID, character: str, replacement: str) -> None:
57
+ generator_entity: Generator = self.generator_repository.find_by_user_id(user_id)
58
+ generator_config_entity: GeneratorConfig = self.generator_config_repository.find_by_generator_id(generator_entity.id)
59
+ generator_config_entity.replace_character(character, replacement)
60
+ generator_config_entity.updated_at = datetime.now(timezone.utc)
61
+ self.generator_config_repository.update(generator_config_entity)
62
+
63
+
64
+ class ResetCharacterReplacementUseCase:
65
+ def __init__(
66
+ self,
67
+ generator_repository: GeneratorRepository,
68
+ generator_config_repository: GeneratorConfigRepository,
69
+ ):
70
+ self.generator_repository: GeneratorRepository = generator_repository
71
+ self.generator_config_repository: GeneratorConfigRepository = generator_config_repository
72
+
73
+ def execute(self, user_id: UUID, character: str) -> None:
74
+ generator_entity: Generator = self.generator_repository.find_by_user_id(user_id)
75
+ generator_config_entity: GeneratorConfig = self.generator_config_repository.find_by_generator_id(generator_entity.id)
76
+ generator_config_entity.reset_character(character)
77
+ generator_config_entity.updated_at = datetime.now(timezone.utc)
78
+ self.generator_config_repository.update(generator_config_entity)
@@ -0,0 +1,118 @@
1
+ from datetime import datetime, timezone
2
+ from uuid import UUID
3
+
4
+ from passphera_core.entities import Password, Generator, User
5
+ from passphera_core.exceptions import EntityNotFoundException
6
+ from passphera_core.interfaces import PasswordRepository, GeneratorRepository, UserRepository
7
+
8
+
9
+ class GeneratePasswordUseCase:
10
+ def __init__(
11
+ self,
12
+ password_repository: PasswordRepository,
13
+ generator_repository: GeneratorRepository,
14
+ user_repository: UserRepository
15
+ ):
16
+ self.password_repository: PasswordRepository = password_repository
17
+ self.generator_repository: GeneratorRepository = generator_repository
18
+ self.user_repository: UserRepository = user_repository
19
+
20
+ def execute(self, user_id: UUID, context: str, text: str) -> Password:
21
+ user_entity: User = self.user_repository.find_by_id(user_id)
22
+ generator_entity: Generator = self.generator_repository.find_by_id(user_entity.generator)
23
+ password: str = generator_entity.generate_password(text)
24
+ password_entity: Password = Password(user_id=user_id, context=context, text=text, password=password)
25
+ password_entity.encrypt()
26
+ self.password_repository.save(password_entity)
27
+ user_entity.add_password(password_entity.id)
28
+ self.user_repository.update(user_entity)
29
+ return password_entity
30
+
31
+
32
+ class GetPasswordByIdUseCase:
33
+ def __init__(self, password_repository: PasswordRepository):
34
+ self.password_repository: PasswordRepository = password_repository
35
+
36
+ def execute(self, password_id: UUID) -> Password:
37
+ password_entity: Password = self.password_repository.find_by_id(password_id)
38
+ if not password_entity:
39
+ raise EntityNotFoundException(password_entity)
40
+ return password_entity
41
+
42
+
43
+ class GetPasswordByContextUseCase:
44
+ def __init__(self, password_repository: PasswordRepository, user_repository: UserRepository):
45
+ self.password_repository: PasswordRepository = password_repository
46
+ self.user_repository: UserRepository = user_repository
47
+
48
+ def execute(self, user_id: UUID, context: str) -> Password:
49
+ user_entity: User = self.user_repository.find_by_id(user_id)
50
+ for password_id in user_entity.passwords:
51
+ password_entity: Password = self.password_repository.find_by_id(password_id)
52
+ if password_entity.context == context:
53
+ return password_entity
54
+ raise EntityNotFoundException(Password())
55
+
56
+
57
+ class UpdatePasswordUseCase:
58
+ def __init__(
59
+ self,
60
+ password_repository: PasswordRepository,
61
+ generator_repository: GeneratorRepository,
62
+ user_repository: UserRepository
63
+ ):
64
+ self.password_repository: PasswordRepository = password_repository
65
+ self.generator_repository: GeneratorRepository = generator_repository
66
+ self.user_repository: UserRepository = user_repository
67
+
68
+ def execute(self, user_id: UUID, context: str, text: str) -> Password:
69
+ user_entity: User = self.user_repository.find_by_id(user_id)
70
+ generator_entity: Generator = self.generator_repository.find_by_id(user_entity.generator)
71
+ for password_id in user_entity.passwords:
72
+ password_entity: Password = self.password_repository.find_by_id(password_id)
73
+ if password_entity.context == context:
74
+ password_entity.password = generator_entity.generate_password(text)
75
+ password_entity.encrypt()
76
+ password_entity.updated_at = datetime.now(timezone.utc)
77
+ self.password_repository.update(password_entity)
78
+ return password_entity
79
+ raise EntityNotFoundException(Password())
80
+
81
+
82
+ class DeletePasswordUseCase:
83
+ def __init__(self, password_repository: PasswordRepository, user_repository: UserRepository):
84
+ self.password_repository: PasswordRepository = password_repository
85
+ self.user_repository: UserRepository = user_repository
86
+
87
+ def execute(self, user_id: UUID, password_id: UUID) -> None:
88
+ self.password_repository.delete(password_id)
89
+ user_entity: User = self.user_repository.find_by_id(user_id)
90
+ user_entity.delete_password(password_id)
91
+ self.user_repository.update(user_entity)
92
+
93
+
94
+ class GetAllUserPasswordsUseCase:
95
+ def __init__(self, password_repository: PasswordRepository, user_repository: UserRepository):
96
+ self.password_repository: PasswordRepository = password_repository
97
+ self.user_repository: UserRepository = user_repository
98
+
99
+ def execute(self, user_id: UUID) -> list[Password]:
100
+ user_entity: User = self.user_repository.find_by_id(user_id)
101
+ passwords: list[Password] = []
102
+ for password_id in user_entity.passwords:
103
+ password_entity: Password = self.password_repository.find_by_id(password_id)
104
+ passwords.append(password_entity)
105
+ return passwords
106
+
107
+
108
+ class DeleteAllUserPasswordsUseCase:
109
+ def __init__(self, password_repository: PasswordRepository, user_repository: UserRepository):
110
+ self.password_repository: PasswordRepository = password_repository
111
+ self.user_repository: UserRepository = user_repository
112
+
113
+ def execute(self, user_id: UUID) -> None:
114
+ user_entity: User = self.user_repository.find_by_id(user_id)
115
+ for password_id in user_entity.passwords:
116
+ self.password_repository.delete(password_id)
117
+ user_entity.delete_password(password_id)
118
+ self.user_repository.update(user_entity)
@@ -0,0 +1,56 @@
1
+ from uuid import UUID
2
+
3
+ from passphera_core.entities import User, Generator
4
+ from passphera_core.exceptions import DuplicateEntityException
5
+ from passphera_core.interfaces import UserRepository, GeneratorRepository
6
+
7
+
8
+ class RegisterUserUseCase:
9
+ def __init__(self, user_repository: UserRepository, generator_repository: GeneratorRepository):
10
+ self.user_repository: UserRepository = user_repository
11
+ self.generator_repository: GeneratorRepository = generator_repository
12
+
13
+ def execute(self, user: User) -> User:
14
+ if self.user_repository.find_by_email(user.email):
15
+ raise DuplicateEntityException(user, 'email')
16
+ if self.user_repository.find_by_username(user.username):
17
+ raise DuplicateEntityException(user, 'username')
18
+ user_entity: User = User(**user.__dict__)
19
+ generator_entity: Generator = Generator(user_id=user_entity.id)
20
+ self.generator_repository.save(generator_entity)
21
+ user_entity.generator = generator_entity.id
22
+ self.user_repository.save(user_entity)
23
+ return user_entity
24
+
25
+
26
+ class GetUserByIdUseCase:
27
+ def __init__(self, user_repository: UserRepository):
28
+ self.user_repository: UserRepository = user_repository
29
+
30
+ def execute(self, id: UUID) -> User:
31
+ user = self.user_repository.find_by_id(id)
32
+ if not user:
33
+ raise ValueError(f'User not found')
34
+ return user
35
+
36
+
37
+ class GetUserByUsernameUseCase:
38
+ def __init__(self, user_repository: UserRepository):
39
+ self.user_repository = user_repository
40
+
41
+ def execute(self, username: str) -> User:
42
+ user = self.user_repository.find_by_username(username)
43
+ if not user:
44
+ raise ValueError(f'User not found')
45
+ return user
46
+
47
+
48
+ class GetUserByEmailUseCase:
49
+ def __init__(self, user_repository: UserRepository):
50
+ self.user_repository = user_repository
51
+
52
+ def execute(self, email: str) -> User:
53
+ user = self.user_repository.find_by_email(email)
54
+ if not user:
55
+ raise ValueError(f'User not found')
56
+ return user
@@ -0,0 +1,125 @@
1
+ from dataclasses import dataclass, field
2
+ from datetime import datetime, timezone
3
+ from uuid import UUID, uuid4
4
+
5
+ from cryptography.fernet import Fernet
6
+
7
+ from cipherspy.cipher import *
8
+ from cipherspy.cipher.base_cipher import BaseCipherAlgorithm
9
+ from cipherspy.utilities import generate_salt, derive_key
10
+
11
+ from passphera_core import exceptions
12
+
13
+
14
+ @dataclass
15
+ class Password:
16
+ id: UUID = field(default_factory=uuid4)
17
+ user_id: UUID = field(default_factory=uuid4)
18
+ created_at: datetime = field(default=datetime.now(timezone.utc))
19
+ updated_at: datetime = field(default=datetime.now(timezone.utc))
20
+ context: str = field(default_factory=str)
21
+ text: str = field(default_factory=str)
22
+ password: str = field(default_factory=str)
23
+ salt: bytes = field(default_factory=bytes)
24
+
25
+ def encrypt(self) -> None:
26
+ self.salt = generate_salt()
27
+ key = derive_key(self.password, self.salt)
28
+ self.password = Fernet(key).encrypt(self.password.encode()).decode()
29
+
30
+ def decrypt(self) -> str:
31
+ key = derive_key(self.password, self.salt)
32
+ return Fernet(key).decrypt(self.password.encode()).decode()
33
+
34
+
35
+ @dataclass
36
+ class GeneratorConfig:
37
+ id: UUID = field(default_factory=uuid4)
38
+ generator_id: UUID = field(default_factory=uuid4)
39
+ shift: int = field(default=3)
40
+ multiplier: int = field(default=3)
41
+ key: str = field(default="hill")
42
+ algorithm: str = field(default="hill")
43
+ prefix: str = field(default="secret")
44
+ postfix: str = field(default="secret")
45
+ characters_replacements: dict[str, str] = field(default_factory=dict[str, str])
46
+ _cipher_registry: dict[str, BaseCipherAlgorithm] = field(default_factory=lambda: {
47
+ 'caesar': CaesarCipherAlgorithm,
48
+ 'affine': AffineCipherAlgorithm,
49
+ 'playfair': PlayfairCipherAlgorithm,
50
+ 'hill': HillCipherAlgorithm,
51
+ }, init=False)
52
+
53
+ def get_algorithm(self) -> BaseCipherAlgorithm:
54
+ """
55
+ Get the primary algorithm used to cipher the password
56
+ :return: BaseCipherAlgorithm: The primary algorithm used for the cipher
57
+ """
58
+ if self.algorithm.lower() not in self._cipher_registry:
59
+ raise exceptions.InvalidAlgorithmException(self.algorithm)
60
+ return self._cipher_registry[self.algorithm.lower()]
61
+
62
+ def replace_character(self, char: str, replacement: str) -> None:
63
+ """
64
+ Replace a character with another character or set of characters
65
+ Eg: pg.replace_character('a', '@1')
66
+ :param char: The character to be replaced
67
+ :param replacement: The (character|set of characters) to replace the first one
68
+ :return:
69
+ """
70
+ self.characters_replacements[char[0]] = replacement
71
+
72
+ def reset_character(self, char: str) -> None:
73
+ """
74
+ Reset a character to its original value (remove its replacement from characters_replacements)
75
+ :param char: The character to be reset to its original value
76
+ :return:
77
+ """
78
+ self.characters_replacements.pop(char, None)
79
+
80
+
81
+ @dataclass
82
+ class Generator:
83
+ id: UUID = field(default_factory=uuid4)
84
+ user_id: UUID = field(default_factory=uuid4)
85
+ created_at: datetime = field(default_factory=datetime.now)
86
+ updated_at: datetime = field(default_factory=datetime.now)
87
+ config: GeneratorConfig = field(default_factory=GeneratorConfig)
88
+
89
+ def apply_replacements(self, password: str) -> str:
90
+ """
91
+ Replace character from the ciphered password with character replacements from the generator configurations
92
+ :return: str: The new ciphered password after character replacements
93
+ """
94
+ translation_table = str.maketrans(self.config.characters_replacements)
95
+ return password.translate(translation_table)
96
+
97
+ def generate_password(self, text: str) -> str:
98
+ """
99
+ Generate a strong password string using the raw password (add another layer of encryption to it)
100
+ :return: str: The generated ciphered password
101
+ """
102
+ affine = AffineCipherAlgorithm(self.config.shift, self.config.multiplier)
103
+ intermediate = affine.encrypt(f"{self.config.prefix}{text}{self.config.postfix}")
104
+ main_algorithm = self.config.get_algorithm()
105
+ password = main_algorithm.encrypt(intermediate)
106
+ password = self.apply_replacements(password)
107
+ return ''.join(c.upper() if c in text else c for c in password)
108
+
109
+
110
+ @dataclass
111
+ class User:
112
+ id: UUID = field(default_factory=uuid4)
113
+ created_at: datetime = field(default_factory=datetime.now)
114
+ updated_at: datetime = field(default_factory=datetime.now)
115
+ username: str = field(default_factory=str)
116
+ email: str = field(default_factory=str)
117
+ password: str = field(default_factory=str)
118
+ generator: UUID = field(default_factory=UUID)
119
+ passwords: list[UUID] = field(default_factory=list[UUID])
120
+
121
+ def add_password(self, password_id: UUID) -> None:
122
+ self.passwords.append(password_id)
123
+
124
+ def delete_password(self, password_id: UUID) -> None:
125
+ self.passwords.remove(password_id)
@@ -1,3 +1,49 @@
1
+ from typing import Union, Literal
2
+
3
+ from passphera_core.entities import Password, Generator, User
4
+
5
+
1
6
  class InvalidAlgorithmException(Exception):
2
7
  def __init__(self, algorithm: str) -> None:
3
- super().__init__(f"Invalid algorithm name [{algorithm}]")
8
+ self.algorithm = algorithm
9
+ super().__init__(f"Invalid algorithm: '{algorithm}'")
10
+
11
+
12
+ class EntityNotFoundException(Exception):
13
+ def __init__(self, entity: Union[Password, Generator, User]) -> None:
14
+ self.entity = entity
15
+ entity_type = entity.__class__.__name__
16
+ super().__init__(f"{entity_type} not found")
17
+
18
+
19
+ class DuplicateEntityException(Exception):
20
+ def __init__(
21
+ self,
22
+ entity: Union[Password, User],
23
+ duplicate_field: Literal['context', 'email', 'username'] = None
24
+ ) -> None:
25
+ self.entity = entity
26
+ self.duplicate_field = duplicate_field
27
+ message = self._build_message(entity, duplicate_field)
28
+ super().__init__(message)
29
+
30
+ def _build_message(self, entity: Union[Password, User], duplicate_field: str | None) -> str:
31
+ if isinstance(entity, Password):
32
+ return self._build_password_message(entity)
33
+ elif isinstance(entity, User):
34
+ return self._build_user_message(entity, duplicate_field)
35
+ return "Duplicate entity detected"
36
+
37
+ @staticmethod
38
+ def _build_password_message(password: Password) -> str:
39
+ if hasattr(password, 'context') and password.context:
40
+ return f"Password for context '{password.context}' already exists"
41
+ return "Duplicate password detected"
42
+
43
+ @staticmethod
44
+ def _build_user_message(user: User, duplicate_field: str) -> str:
45
+ if duplicate_field == 'email':
46
+ return f"User with email '{user.email}' already exists"
47
+ elif duplicate_field == 'username':
48
+ return f"User with username '{user.username}' already exists"
49
+ return "Duplicate user detected"
@@ -0,0 +1,92 @@
1
+ from abc import ABC, abstractmethod
2
+ from uuid import UUID
3
+
4
+ from passphera_core.entities import Password, Generator, GeneratorConfig, User
5
+
6
+
7
+ class PasswordRepository(ABC):
8
+ @abstractmethod
9
+ def save(self, password: Password) -> None:
10
+ pass
11
+
12
+ @abstractmethod
13
+ def update(self, password: Password) -> None:
14
+ pass
15
+
16
+ @abstractmethod
17
+ def delete(self, password_id: UUID) -> None:
18
+ pass
19
+
20
+ @abstractmethod
21
+ def find_by_id(self, password_id: UUID) -> Password:
22
+ pass
23
+
24
+
25
+ class GeneratorRepository(ABC):
26
+ @abstractmethod
27
+ def save(self, generator: Generator) -> None:
28
+ pass
29
+
30
+ @abstractmethod
31
+ def update(self, generator: Generator) -> None:
32
+ pass
33
+
34
+ @abstractmethod
35
+ def delete(self, generator_id: UUID) -> None:
36
+ pass
37
+
38
+ @abstractmethod
39
+ def find_by_id(self, generator_id: UUID) -> Generator:
40
+ pass
41
+
42
+ @abstractmethod
43
+ def find_by_user_id(self, user_id: UUID) -> Generator:
44
+ pass
45
+
46
+
47
+ class GeneratorConfigRepository(ABC):
48
+ @abstractmethod
49
+ def save(self, generator_config: GeneratorConfig) -> None:
50
+ pass
51
+
52
+ @abstractmethod
53
+ def update(self, generator_config: GeneratorConfig) -> None:
54
+ pass
55
+
56
+ @abstractmethod
57
+ def delete(self, generator_config_id: UUID) -> None:
58
+ pass
59
+
60
+ @abstractmethod
61
+ def find_by_id(self, generator_config_id: UUID) -> GeneratorConfig:
62
+ pass
63
+
64
+ @abstractmethod
65
+ def find_by_generator_id(self, generator_id: UUID) -> GeneratorConfig:
66
+ pass
67
+
68
+
69
+ class UserRepository(ABC):
70
+ @abstractmethod
71
+ def save(self, user: User) -> None:
72
+ pass
73
+
74
+ @abstractmethod
75
+ def update(self, user: User) -> None:
76
+ pass
77
+
78
+ @abstractmethod
79
+ def delete(self, user_id: UUID) -> None:
80
+ pass
81
+
82
+ @abstractmethod
83
+ def find_by_id(self, user_id: UUID) -> User:
84
+ pass
85
+
86
+ @abstractmethod
87
+ def find_by_username(self, username: str) -> User:
88
+ pass
89
+
90
+ @abstractmethod
91
+ def find_by_email(self, email: str) -> User:
92
+ pass
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: passphera-core
3
- Version: 0.8.2
3
+ Version: 0.9.1
4
4
  Summary: The core system of passphera project
5
5
  Home-page: https://github.com/passphera/core
6
6
  Author: Fathi Abdelmalek
7
7
  Author-email: abdelmalek.fathi.2001@gmail.com
8
- Classifier: Development Status :: 2 - Pre-Alpha
8
+ Classifier: Development Status :: 3 - Alpha
9
9
  Classifier: License :: OSI Approved :: MIT License
10
10
  Classifier: Operating System :: OS Independent
11
11
  Classifier: Programming Language :: Python
@@ -0,0 +1,12 @@
1
+ passphera_core/__init__.py,sha256=0kEfHV5WvMWDoFn1qCeuD52WsgaX4sQ4V9P48cnFKb8,120
2
+ passphera_core/entities.py,sha256=SvzBKDx9NkaVunXBGT-PgEWbXNXivb8pGw9Q_KFtAgI,5021
3
+ passphera_core/exceptions.py,sha256=5NvV6LW4Pdok6gRxvM0cN3q6JEKgaCYAX8qGr2eqX2w,1890
4
+ passphera_core/interfaces.py,sha256=uwHjPd1uhnA02X9h39IrHuNS-_M8j_RyKMVdxi3DUak,2036
5
+ passphera_core/application/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ passphera_core/application/generator.py,sha256=ABUigLMtP52CHpTKIZmwNb0pgGeL4s_WgrpxLr1M9zw,3896
7
+ passphera_core/application/password.py,sha256=72PXsHsSYvBDLdzwtC8icwf8KZPScWBKb9jKIQRAkEg,5511
8
+ passphera_core/application/user.py,sha256=WTDjccHjxoqsfC2j0c9MfwREmiST9XxYOHoaDWYGTcg,2066
9
+ passphera_core-0.9.1.dist-info/METADATA,sha256=jwBmrk5oor-rOcC6PSjASjC00mpNFHc3PifeR3uKZ44,671
10
+ passphera_core-0.9.1.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
11
+ passphera_core-0.9.1.dist-info/top_level.txt,sha256=aDUX2iWGOyfzyf6XakLWTbgeWqNrypMHO074Qratyds,15
12
+ passphera_core-0.9.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: bdist_wheel (0.45.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,228 +0,0 @@
1
- from cipherspy.cipher import *
2
-
3
- from .exceptions import InvalidAlgorithmException
4
-
5
-
6
- class PasswordGenerator:
7
- """
8
- A strong password generator use multiple cipher algorithms to cipher a given plain text
9
- """
10
- def __init__(
11
- self,
12
- shift: int = 3,
13
- multiplier: int = 3,
14
- key: str = "hill",
15
- algorithm: str = 'hill',
16
- prefix: str = 'secret',
17
- postfix: str = 'secret',
18
- characters_replacements: dict = None,
19
- ):
20
- """
21
- :param shift: number of characters to shift each character (default 3)
22
- :param multiplier: number of characters to shift each character (default 3)
23
- :param key: cipher key string (default "secret")
24
- :param algorithm: main cipher algorithm name (default 'playfair')
25
- :param characters_replacements: replace characters with the given values (default {})
26
- :param text: plain text to be ciphered
27
- """
28
- if characters_replacements is None:
29
- characters_replacements = {}
30
- self._shift: int = shift
31
- self._multiplier: int = multiplier
32
- self._key: str = key
33
- self._algorithm_name: str = algorithm.lower()
34
- self._algorithm = self._set_algorithm()
35
- self._prefix: str = prefix
36
- self._postfix: str = postfix
37
- self._characters_replacements: dict = characters_replacements
38
-
39
- @property
40
- def shift(self) -> int:
41
- """
42
- Returns the shift value for the cipher algorithm
43
- Eg: ```shift = pg.shift```
44
- :return: int: The shift value for the cipher algorithm
45
- """
46
- return self._shift
47
-
48
- @shift.setter
49
- def shift(self, shift: int) -> None:
50
- """
51
- Sets the shift value for the cipher algorithm
52
- Eg: ```pg.shift = 3```
53
- :param shift: The shift value for the cipher algorithm
54
- :return:
55
- """
56
- self._shift = shift
57
-
58
- @property
59
- def multiplier(self) -> int:
60
- """
61
- Returns the multiplier value for the cipher algorithm
62
- Eg: ```multiplier = pg.multiplier```
63
- :return: int: The multiplier value for the cipher algorithm
64
- """
65
- return self._multiplier
66
-
67
- @multiplier.setter
68
- def multiplier(self, multiplier: int) -> None:
69
- """
70
- Sets the multiplier value for the cipher algorithm
71
- Eg: ```pg.multiplier = 3```
72
- :param multiplier: The multiplier value for the cipher algorithm
73
- :return:
74
- """
75
- self._multiplier = multiplier
76
-
77
- @property
78
- def key(self) -> str:
79
- """
80
- Returns the key string for the cipher algorithm
81
- Eg: ```key = pg.key```
82
- :return: str: The key string for the cipher algorithm
83
- """
84
- return self._key
85
-
86
- @key.setter
87
- def key(self, key: str) -> None:
88
- """
89
- Sets the key string for the cipher algorithm
90
- Eg: ```pg.key = 'secret key'```
91
- :param key: The key string for the cipher algorithm
92
- :return:
93
- """
94
- self._key = key
95
-
96
- @property
97
- def prefix(self) -> str:
98
- """
99
- Returns the prefix string for the cipher algorithm
100
- Eg: ```prefix = pg.prefix```
101
- :return: str: The prefix string for the cipher algorithm
102
- """
103
- return self._prefix
104
-
105
- @prefix.setter
106
- def prefix(self, prefix: str):
107
- """
108
- Sets the prefix string for the cipher algorithm
109
- Eg: ```pg.prefix = 'something'```
110
- :param prefix: The string for the cipher algorithm
111
- :return:
112
- """
113
- self._prefix = prefix
114
-
115
- @property
116
- def postfix(self) -> str:
117
- """
118
- Returns the postfix string for the cipher algorithm
119
- Eg: ```postfix = pg.postfix```
120
- :return: str: The postfix string for the cipher algorithm
121
- """
122
- return self._postfix
123
-
124
- @postfix.setter
125
- def postfix(self, postfix: str):
126
- """
127
- Sets the postfix string for the cipher algorithm
128
- Eg: ```pg.postfix = 'something'```
129
- :param postfix: The string for the cipher algorithm
130
- :return:
131
- """
132
- self._postfix = postfix
133
-
134
- @property
135
- def algorithm(self) -> str:
136
- """
137
- Returns the main cipher algorithm name
138
- Eg: ```algorithm = pg.algorithm```
139
- :return: str: The main cipher algorithm name
140
- """
141
- return self._algorithm_name
142
-
143
- @algorithm.setter
144
- def algorithm(self, algorithm: str) -> None:
145
- """
146
- Sets the main cipher algorithm
147
- Eg: ```pg.algorithm = 'playfair'```
148
- :param algorithm: The name of the main cipher algorithm
149
- :return:
150
- """
151
- self._algorithm_name = algorithm.lower()
152
- self._algorithm = self._set_algorithm()
153
-
154
- @property
155
- def characters_replacements(self) -> dict:
156
- """
157
- Returns the dictionary of the characters replacements
158
- Eg: ```print(pg.characters_replacements) # {'a': '@1', 'b': '#2'}```
159
- :return: dict: The dictionary of the characters replacements
160
- """
161
- return self._characters_replacements
162
-
163
- def _set_algorithm(self):
164
- """
165
- Return new instance of the used algorithm to the given one by it's name
166
- :return: new algorithm class
167
- """
168
- match self._algorithm_name:
169
- case 'caesar':
170
- return CaesarCipher(self._shift)
171
- case 'affine':
172
- return AffineCipher(self._shift, self._multiplier)
173
- case 'playfair':
174
- return PlayfairCipher(self._key)
175
- case 'hill':
176
- return HillCipher(self._key)
177
- case _:
178
- raise InvalidAlgorithmException(self._algorithm_name)
179
-
180
- def _update_algorithm_properties(self) -> None:
181
- """
182
- Update the main cipher algorithm
183
- """
184
- self._algorithm = self._set_algorithm()
185
-
186
- def replace_character(self, char: str, replacement: str) -> None:
187
- """
188
- Replace a character with another character or set of characters
189
- Eg: pg.replace_character('a', '@1')
190
- :param char: The character to be replaced
191
- :param replacement: The (character|set of characters) to replace the first one
192
- :return:
193
- """
194
- self._characters_replacements[char[0]] = replacement
195
-
196
- def reset_character(self, char: str) -> None:
197
- """
198
- Reset a character to it's original value (remove it's replacement from characters_replacements)
199
- :param char: The character to be reset to its original value
200
- :return:
201
- """
202
- if char in self._characters_replacements:
203
- del self._characters_replacements[char]
204
-
205
- def generate_raw_password(self, text: str) -> str:
206
- """
207
- Generate a raw password string using the given parameters
208
- :return: str: The generated raw password
209
- """
210
- self._update_algorithm_properties()
211
- return self._algorithm.encrypt(f"{self._prefix}{text}{self._postfix}")
212
-
213
- def generate_password(self, text: str) -> str:
214
- """
215
- Generate a strong password string using the raw password (add another layer of encryption to it)
216
- :return: str: The generated strong password
217
- """
218
- old_algorithm = self._algorithm_name
219
- self._algorithm_name = 'affine'
220
- password = self.generate_raw_password(text)
221
- self._algorithm_name = old_algorithm
222
- password = self.generate_raw_password(password)
223
- for char, replacement in self._characters_replacements.items():
224
- password = password.replace(char, replacement)
225
- for char in password:
226
- if char in text:
227
- password = password.replace(char, char.upper())
228
- return password
@@ -1,7 +0,0 @@
1
- passphera_core/__init__.py,sha256=ND2D_sTQk5mroNRsKm03PXhx0HNf0740hOtZ4_U98OQ,119
2
- passphera_core/exceptions.py,sha256=PPZhy05QMVnN3mm6EGj-RgWanMaF3E6aKN4lcpNOJY8,158
3
- passphera_core/generator.py,sha256=O255qvsmanrj9BmJX-JxpT5iMFd1iEz2ewJC-r5O1WQ,7726
4
- passphera_core-0.8.2.dist-info/METADATA,sha256=2K4lTWVwrrttr61xA84AZrB6uSD7A1oncNJk-IUOKJE,675
5
- passphera_core-0.8.2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
- passphera_core-0.8.2.dist-info/top_level.txt,sha256=aDUX2iWGOyfzyf6XakLWTbgeWqNrypMHO074Qratyds,15
7
- passphera_core-0.8.2.dist-info/RECORD,,