maleo-foundation 0.2.16__py3-none-any.whl → 0.2.17__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. maleo_foundation/client/services/encryption/aes.py +41 -11
  2. maleo_foundation/client/services/encryption/rsa.py +59 -16
  3. maleo_foundation/client/services/hash/bcrypt.py +24 -9
  4. maleo_foundation/client/services/hash/hmac.py +26 -8
  5. maleo_foundation/client/services/hash/sha256.py +16 -6
  6. maleo_foundation/client/services/key.py +34 -12
  7. maleo_foundation/client/services/signature.py +45 -12
  8. maleo_foundation/client/services/token.py +68 -16
  9. maleo_foundation/expanded_types/client.py +6 -3
  10. maleo_foundation/expanded_types/encryption/aes.py +2 -1
  11. maleo_foundation/expanded_types/encryption/rsa.py +2 -1
  12. maleo_foundation/expanded_types/general.py +2 -1
  13. maleo_foundation/expanded_types/hash.py +2 -1
  14. maleo_foundation/expanded_types/key.py +2 -1
  15. maleo_foundation/expanded_types/query.py +6 -3
  16. maleo_foundation/expanded_types/service.py +6 -3
  17. maleo_foundation/expanded_types/signature.py +2 -1
  18. maleo_foundation/expanded_types/token.py +2 -1
  19. maleo_foundation/managers/client/base.py +5 -1
  20. maleo_foundation/managers/client/google/storage.py +5 -1
  21. maleo_foundation/managers/client/maleo.py +20 -4
  22. maleo_foundation/managers/db.py +21 -6
  23. maleo_foundation/managers/service.py +108 -23
  24. maleo_foundation/middlewares/authentication.py +35 -8
  25. maleo_foundation/middlewares/base.py +53 -15
  26. maleo_foundation/models/table.py +21 -4
  27. maleo_foundation/models/transfers/parameters/service.py +30 -4
  28. maleo_foundation/utils/controller.py +23 -6
  29. maleo_foundation/utils/dependencies/auth.py +14 -4
  30. maleo_foundation/utils/exceptions.py +21 -6
  31. maleo_foundation/utils/loaders/credential/google.py +3 -1
  32. maleo_foundation/utils/query.py +38 -7
  33. {maleo_foundation-0.2.16.dist-info → maleo_foundation-0.2.17.dist-info}/METADATA +1 -1
  34. {maleo_foundation-0.2.16.dist-info → maleo_foundation-0.2.17.dist-info}/RECORD +36 -36
  35. {maleo_foundation-0.2.16.dist-info → maleo_foundation-0.2.17.dist-info}/WHEEL +0 -0
  36. {maleo_foundation-0.2.16.dist-info → maleo_foundation-0.2.17.dist-info}/top_level.txt +0 -0
@@ -2,15 +2,22 @@ import os
2
2
  from base64 import b64decode, b64encode
3
3
  from cryptography.hazmat.backends import default_backend
4
4
  from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
5
- from maleo_foundation.expanded_types.encryption.aes import MaleoFoundationAESEncryptionResultsTypes
5
+ from maleo_foundation.expanded_types.encryption.aes \
6
+ import MaleoFoundationAESEncryptionResultsTypes
6
7
  from maleo_foundation.managers.client.base import ClientService
7
- from maleo_foundation.models.schemas.encryption import MaleoFoundationEncryptionSchemas
8
- from maleo_foundation.models.transfers.parameters.encryption.aes import MaleoFoundationAESEncryptionParametersTransfers
9
- from maleo_foundation.models.transfers.results.encryption.aes import EncryptData, MaleoFoundationAESEncryptionResultsTransfers
8
+ from maleo_foundation.models.schemas.encryption \
9
+ import MaleoFoundationEncryptionSchemas
10
+ from maleo_foundation.models.transfers.parameters.encryption.aes \
11
+ import MaleoFoundationAESEncryptionParametersTransfers
12
+ from maleo_foundation.models.transfers.results.encryption.aes \
13
+ import EncryptData, MaleoFoundationAESEncryptionResultsTransfers
10
14
  from maleo_foundation.utils.exceptions import BaseExceptions
11
15
 
12
16
  class MaleoFoundationAESEncryptionClientService(ClientService):
13
- def encrypt(self, parameters:MaleoFoundationAESEncryptionParametersTransfers.Encrypt) -> MaleoFoundationAESEncryptionResultsTypes.Encrypt:
17
+ def encrypt(
18
+ self,
19
+ parameters:MaleoFoundationAESEncryptionParametersTransfers.Encrypt
20
+ ) -> MaleoFoundationAESEncryptionResultsTypes.Encrypt:
14
21
  """Encrypt a plaintext using AES algorithm."""
15
22
  @BaseExceptions.service_exception_handler(
16
23
  operation="encrypting plaintext",
@@ -22,18 +29,34 @@ class MaleoFoundationAESEncryptionClientService(ClientService):
22
29
  key_bytes = os.urandom(32)
23
30
  initialization_vector_bytes = os.urandom(16)
24
31
  #* Encrypt message with encryptor instance
25
- cipher = Cipher(algorithms.AES(key_bytes), modes.CFB(initialization_vector_bytes), backend=default_backend())
32
+ cipher = Cipher(
33
+ algorithm=algorithms.AES(key_bytes),
34
+ mode=modes.CFB(initialization_vector_bytes),
35
+ backend=default_backend()
36
+ )
26
37
  encryptor = cipher.encryptor()
27
- ciphertext = b64encode(encryptor.update(parameters.plaintext.encode()) + encryptor.finalize()).decode('utf-8')
38
+ ciphertext = (
39
+ b64encode(
40
+ encryptor
41
+ .update(parameters.plaintext.encode()) + encryptor.finalize()
42
+ )
43
+ .decode('utf-8'))
28
44
  #* Encode the results to base64 strings
29
45
  key = b64encode(key_bytes).decode('utf-8')
30
46
  initialization_vector = b64encode(initialization_vector_bytes).decode('utf-8')
31
- data = EncryptData(key=key, initialization_vector=initialization_vector, ciphertext=ciphertext)
47
+ data = EncryptData(
48
+ key=key,
49
+ initialization_vector=initialization_vector,
50
+ ciphertext=ciphertext
51
+ )
32
52
  self._logger.info("Plaintext successfully encrypted")
33
53
  return MaleoFoundationAESEncryptionResultsTransfers.Encrypt(data=data)
34
54
  return _impl()
35
55
 
36
- def decrypt(self, parameters:MaleoFoundationAESEncryptionParametersTransfers.Decrypt) -> MaleoFoundationAESEncryptionResultsTypes.Decrypt:
56
+ def decrypt(
57
+ self,
58
+ parameters:MaleoFoundationAESEncryptionParametersTransfers.Decrypt
59
+ ) -> MaleoFoundationAESEncryptionResultsTypes.Decrypt:
37
60
  """Decrypt a ciphertext using AES algorithm."""
38
61
  @BaseExceptions.service_exception_handler(
39
62
  operation="verify single encryption",
@@ -45,9 +68,16 @@ class MaleoFoundationAESEncryptionClientService(ClientService):
45
68
  key_bytes = b64decode(parameters.key)
46
69
  initialization_vector_bytes = b64decode(parameters.initialization_vector)
47
70
  #* Decrypt message with decryptor instance
48
- cipher = Cipher(algorithms.AES(key_bytes), modes.CFB(initialization_vector_bytes), backend=default_backend())
71
+ cipher = Cipher(
72
+ algorithm=algorithms.AES(key_bytes),
73
+ mode=modes.CFB(initialization_vector_bytes),
74
+ backend=default_backend()
75
+ )
49
76
  decryptor = cipher.decryptor()
50
- plaintext = decryptor.update(b64decode(parameters.ciphertext)) + decryptor.finalize()
77
+ plaintext = (
78
+ decryptor
79
+ .update(b64decode(parameters.ciphertext)) + decryptor.finalize()
80
+ )
51
81
  data = MaleoFoundationEncryptionSchemas.Plaintext(plaintext=plaintext)
52
82
  self._logger.info("Ciphertext successfully decrypted")
53
83
  return MaleoFoundationAESEncryptionResultsTransfers.Decrypt(data=data)
@@ -2,16 +2,23 @@ from base64 import b64decode, b64encode
2
2
  from Crypto.Cipher import PKCS1_OAEP
3
3
  from Crypto.Hash import SHA256
4
4
  from maleo_foundation.enums import BaseEnums
5
- from maleo_foundation.expanded_types.encryption.rsa import MaleoFoundationRSAEncryptionResultsTypes
5
+ from maleo_foundation.expanded_types.encryption.rsa \
6
+ import MaleoFoundationRSAEncryptionResultsTypes
6
7
  from maleo_foundation.managers.client.base import ClientService
7
- from maleo_foundation.models.schemas.encryption import MaleoFoundationEncryptionSchemas
8
- from maleo_foundation.models.transfers.parameters.encryption.rsa import MaleoFoundationRSAEncryptionParametersTransfers
9
- from maleo_foundation.models.transfers.results.encryption.rsa import MaleoFoundationRSAEncryptionResultsTransfers
8
+ from maleo_foundation.models.schemas.encryption \
9
+ import MaleoFoundationEncryptionSchemas
10
+ from maleo_foundation.models.transfers.parameters.encryption.rsa \
11
+ import MaleoFoundationRSAEncryptionParametersTransfers
12
+ from maleo_foundation.models.transfers.results.encryption.rsa \
13
+ import MaleoFoundationRSAEncryptionResultsTransfers
10
14
  from maleo_foundation.utils.exceptions import BaseExceptions
11
15
  from maleo_foundation.utils.loaders.key.rsa import RSAKeyLoader
12
16
 
13
17
  class MaleoFoundationRSAEncryptionClientService(ClientService):
14
- def encrypt(self, parameters:MaleoFoundationRSAEncryptionParametersTransfers.Encrypt) -> MaleoFoundationRSAEncryptionResultsTypes.Encrypt:
18
+ def encrypt(
19
+ self,
20
+ parameters:MaleoFoundationRSAEncryptionParametersTransfers.Encrypt
21
+ ) -> MaleoFoundationRSAEncryptionResultsTypes.Encrypt:
15
22
  """Encrypt a plaintext using RSA algorithm."""
16
23
  @BaseExceptions.service_exception_handler(
17
24
  operation="encrypting plaintext",
@@ -20,26 +27,48 @@ class MaleoFoundationRSAEncryptionClientService(ClientService):
20
27
  )
21
28
  def _impl():
22
29
  try:
23
- public_key = RSAKeyLoader.load_with_pycryptodome(type=BaseEnums.KeyType.PUBLIC, extern_key=parameters.key)
30
+ public_key = RSAKeyLoader.load_with_pycryptodome(
31
+ type=BaseEnums.KeyType.PUBLIC,
32
+ extern_key=parameters.key
33
+ )
24
34
  except TypeError:
25
35
  message = "Invalid key type"
26
36
  description = "A public key must be used for encrypting a plaintext"
27
37
  other = "Ensure the given key is of type public key"
28
- return MaleoFoundationRSAEncryptionResultsTransfers.Fail(message=message, description=description, other=other)
38
+ return MaleoFoundationRSAEncryptionResultsTransfers.Fail(
39
+ message=message,
40
+ description=description,
41
+ other=other
42
+ )
29
43
  except Exception as e:
30
44
  self._logger.error("Unexpected error occured while trying to import key:\n'%s'", str(e), exc_info=True)
31
45
  message = "Invalid key"
32
46
  description = "Unexpected error occured while trying to import key"
33
47
  other = "Ensure given key is valid"
34
- return MaleoFoundationRSAEncryptionResultsTransfers.Fail(message=message, description=description, other=other)
35
- cipher = PKCS1_OAEP.new(public_key, hashAlgo=SHA256) #* Initialize cipher with OAEP padding and SHA-256
36
- ciphertext = b64encode(cipher.encrypt(parameters.plaintext.encode('utf-8'))).decode('utf-8') #* Encrypt the plaintext and return as base64-encoded string
48
+ return MaleoFoundationRSAEncryptionResultsTransfers.Fail(
49
+ message=message,
50
+ description=description,
51
+ other=other
52
+ )
53
+ #* Initialize cipher with OAEP padding and SHA-256
54
+ cipher = PKCS1_OAEP.new(public_key, hashAlgo=SHA256)
55
+ #* Encrypt the plaintext and return as base64-encoded string
56
+ ciphertext = (
57
+ b64encode(
58
+ cipher
59
+ .encrypt(parameters.plaintext.encode('utf-8'))
60
+ )
61
+ .decode('utf-8')
62
+ )
37
63
  data = MaleoFoundationEncryptionSchemas.Ciphertext(ciphertext=ciphertext)
38
64
  self._logger.info("Plaintext successfully encrypted")
39
65
  return MaleoFoundationRSAEncryptionResultsTransfers.Encrypt(data=data)
40
66
  return _impl()
41
67
 
42
- def decrypt(self, parameters:MaleoFoundationRSAEncryptionParametersTransfers.Decrypt) -> MaleoFoundationRSAEncryptionResultsTypes.Decrypt:
68
+ def decrypt(
69
+ self,
70
+ parameters:MaleoFoundationRSAEncryptionParametersTransfers.Decrypt
71
+ ) -> MaleoFoundationRSAEncryptionResultsTypes.Decrypt:
43
72
  """Decrypt a ciphertext using RSA algorithm."""
44
73
  @BaseExceptions.service_exception_handler(
45
74
  operation="verify single encryption",
@@ -48,20 +77,34 @@ class MaleoFoundationRSAEncryptionClientService(ClientService):
48
77
  )
49
78
  def _impl():
50
79
  try:
51
- private_key = RSAKeyLoader.load_with_pycryptodome(type=BaseEnums.KeyType.PRIVATE, extern_key=parameters.key, passphrase=parameters.password)
80
+ private_key = RSAKeyLoader.load_with_pycryptodome(
81
+ type=BaseEnums.KeyType.PRIVATE,
82
+ extern_key=parameters.key,
83
+ passphrase=parameters.password
84
+ )
52
85
  except TypeError:
53
86
  message = "Invalid key type"
54
87
  description = "A private key must be used for decrypting a ciphertext"
55
88
  other = "Ensure the given key is of type private key"
56
- return MaleoFoundationRSAEncryptionResultsTransfers.Fail(message=message, description=description, other=other)
89
+ return MaleoFoundationRSAEncryptionResultsTransfers.Fail(
90
+ message=message,
91
+ description=description,
92
+ other=other
93
+ )
57
94
  except Exception as e:
58
95
  self._logger.error("Unexpected error occured while trying to import key:\n'%s'", str(e), exc_info=True)
59
96
  message = "Invalid key"
60
97
  description = "Unexpected error occured while trying to import key"
61
98
  other = "Ensure given key is valid"
62
- return MaleoFoundationRSAEncryptionResultsTransfers.Fail(message=message, description=description, other=other)
63
- cipher = PKCS1_OAEP.new(private_key, hashAlgo=SHA256) #* Initialize cipher with OAEP padding and SHA-256
64
- plaintext = cipher.decrypt(b64decode(parameters.ciphertext)) #* Decode the base64-encoded ciphertext and then decrypt
99
+ return MaleoFoundationRSAEncryptionResultsTransfers.Fail(
100
+ message=message,
101
+ description=description,
102
+ other=other
103
+ )
104
+ #* Initialize cipher with OAEP padding and SHA-256
105
+ cipher = PKCS1_OAEP.new(private_key, hashAlgo=SHA256)
106
+ #* Decode the base64-encoded ciphertext and then decrypt
107
+ plaintext = cipher.decrypt(b64decode(parameters.ciphertext))
65
108
  data = MaleoFoundationEncryptionSchemas.Plaintext(plaintext=plaintext)
66
109
  self._logger.info("Ciphertext successfully decrypted")
67
110
  return MaleoFoundationRSAEncryptionResultsTransfers.Decrypt(data=data)
@@ -1,13 +1,20 @@
1
1
  import bcrypt
2
- from maleo_foundation.expanded_types.hash import MaleoFoundationHashResultsTypes
2
+ from maleo_foundation.expanded_types.hash \
3
+ import MaleoFoundationHashResultsTypes
3
4
  from maleo_foundation.managers.client.base import ClientService
4
- from maleo_foundation.models.schemas.hash import MaleoFoundationHashSchemas
5
- from maleo_foundation.models.transfers.parameters.hash.bcrypt import MaleoFoundationBcryptHashParametersTransfers
6
- from maleo_foundation.models.transfers.results.hash import MaleoFoundationHashResultsTransfers
5
+ from maleo_foundation.models.schemas.hash import \
6
+ MaleoFoundationHashSchemas
7
+ from maleo_foundation.models.transfers.parameters.hash.bcrypt \
8
+ import MaleoFoundationBcryptHashParametersTransfers
9
+ from maleo_foundation.models.transfers.results.hash \
10
+ import MaleoFoundationHashResultsTransfers
7
11
  from maleo_foundation.utils.exceptions import BaseExceptions
8
12
 
9
13
  class MaleoFoundationBcryptHashClientService(ClientService):
10
- def hash(self, parameters:MaleoFoundationBcryptHashParametersTransfers.Hash) -> MaleoFoundationHashResultsTypes.Hash:
14
+ def hash(
15
+ self,
16
+ parameters:MaleoFoundationBcryptHashParametersTransfers.Hash
17
+ ) -> MaleoFoundationHashResultsTypes.Hash:
11
18
  """Generate a bcrypt hash for the given message."""
12
19
  @BaseExceptions.service_exception_handler(
13
20
  operation="hashing single message",
@@ -15,14 +22,19 @@ class MaleoFoundationBcryptHashClientService(ClientService):
15
22
  fail_result_class=MaleoFoundationHashResultsTransfers.Fail
16
23
  )
17
24
  def _impl():
18
- salt = bcrypt.gensalt()
19
- hash = bcrypt.hashpw(parameters.message.encode(), salt).decode()
25
+ hash = bcrypt.hashpw(
26
+ password=parameters.message.encode(),
27
+ salt=bcrypt.gensalt()
28
+ ).decode()
20
29
  data = MaleoFoundationHashSchemas.Hash(hash=hash)
21
30
  self._logger.info("Message successfully hashed")
22
31
  return MaleoFoundationHashResultsTransfers.Hash(data=data)
23
32
  return _impl()
24
33
 
25
- def verify(self, parameters:MaleoFoundationBcryptHashParametersTransfers.Verify) -> MaleoFoundationHashResultsTypes.Verify:
34
+ def verify(
35
+ self,
36
+ parameters:MaleoFoundationBcryptHashParametersTransfers.Verify
37
+ ) -> MaleoFoundationHashResultsTypes.Verify:
26
38
  """Verify a message against the given message hash."""
27
39
  @BaseExceptions.service_exception_handler(
28
40
  operation="verify single hash",
@@ -30,7 +42,10 @@ class MaleoFoundationBcryptHashClientService(ClientService):
30
42
  fail_result_class=MaleoFoundationHashResultsTransfers.Fail
31
43
  )
32
44
  def _impl():
33
- is_valid = bcrypt.checkpw(parameters.message.encode(), parameters.hash.encode())
45
+ is_valid = bcrypt.checkpw(
46
+ password=parameters.message.encode(),
47
+ hashed_password=parameters.hash.encode()
48
+ )
34
49
  data = MaleoFoundationHashSchemas.IsValid(is_valid=is_valid)
35
50
  self._logger.info("Hash successfully verified")
36
51
  return MaleoFoundationHashResultsTransfers.Verify(data=data)
@@ -1,13 +1,20 @@
1
1
  from Crypto.Hash import HMAC, SHA256
2
- from maleo_foundation.expanded_types.hash import MaleoFoundationHashResultsTypes
2
+ from maleo_foundation.expanded_types.hash \
3
+ import MaleoFoundationHashResultsTypes
3
4
  from maleo_foundation.managers.client.base import ClientService
4
- from maleo_foundation.models.schemas.hash import MaleoFoundationHashSchemas
5
- from maleo_foundation.models.transfers.parameters.hash.hmac import MaleoFoundationHMACHashParametersTransfers
6
- from maleo_foundation.models.transfers.results.hash import MaleoFoundationHashResultsTransfers
5
+ from maleo_foundation.models.schemas.hash import \
6
+ MaleoFoundationHashSchemas
7
+ from maleo_foundation.models.transfers.parameters.hash.hmac \
8
+ import MaleoFoundationHMACHashParametersTransfers
9
+ from maleo_foundation.models.transfers.results.hash \
10
+ import MaleoFoundationHashResultsTransfers
7
11
  from maleo_foundation.utils.exceptions import BaseExceptions
8
12
 
9
13
  class MaleoFoundationHMACHashClientService(ClientService):
10
- def hash(self, parameters:MaleoFoundationHMACHashParametersTransfers.Hash) -> MaleoFoundationHashResultsTypes.Hash:
14
+ def hash(
15
+ self,
16
+ parameters:MaleoFoundationHMACHashParametersTransfers.Hash
17
+ ) -> MaleoFoundationHashResultsTypes.Hash:
11
18
  """Generate a hmac hash for the given message."""
12
19
  @BaseExceptions.service_exception_handler(
13
20
  operation="hashing single message",
@@ -15,13 +22,20 @@ class MaleoFoundationHMACHashClientService(ClientService):
15
22
  fail_result_class=MaleoFoundationHashResultsTransfers.Fail
16
23
  )
17
24
  def _impl():
18
- hash = HMAC.new(parameters.key.encode(), parameters.message.encode(), SHA256).hexdigest()
25
+ hash = HMAC.new(
26
+ key=parameters.key.encode(),
27
+ msg=parameters.message.encode(),
28
+ digestmod=SHA256
29
+ ).hexdigest()
19
30
  data = MaleoFoundationHashSchemas.Hash(hash=hash)
20
31
  self._logger.info("Message successfully hashed")
21
32
  return MaleoFoundationHashResultsTransfers.Hash(data=data)
22
33
  return _impl()
23
34
 
24
- def verify(self, parameters:MaleoFoundationHMACHashParametersTransfers.Verify) -> MaleoFoundationHashResultsTypes.Verify:
35
+ def verify(
36
+ self,
37
+ parameters:MaleoFoundationHMACHashParametersTransfers.Verify
38
+ ) -> MaleoFoundationHashResultsTypes.Verify:
25
39
  """Verify a message against the given message hash."""
26
40
  @BaseExceptions.service_exception_handler(
27
41
  operation="verify single hash",
@@ -29,7 +43,11 @@ class MaleoFoundationHMACHashClientService(ClientService):
29
43
  fail_result_class=MaleoFoundationHashResultsTransfers.Fail
30
44
  )
31
45
  def _impl():
32
- computed_hash = HMAC.new(parameters.key.encode(), parameters.message.encode(), SHA256).hexdigest()
46
+ computed_hash = HMAC.new(
47
+ key=parameters.key.encode(),
48
+ msg=parameters.message.encode(),
49
+ digestmod=SHA256
50
+ ).hexdigest()
33
51
  is_valid = computed_hash == parameters.hash
34
52
  data = MaleoFoundationHashSchemas.IsValid(is_valid=is_valid)
35
53
  self._logger.info("Hash successfully verified")
@@ -1,13 +1,20 @@
1
1
  from Crypto.Hash import SHA256
2
- from maleo_foundation.expanded_types.hash import MaleoFoundationHashResultsTypes
2
+ from maleo_foundation.expanded_types.hash \
3
+ import MaleoFoundationHashResultsTypes
3
4
  from maleo_foundation.managers.client.base import ClientService
4
- from maleo_foundation.models.schemas.hash import MaleoFoundationHashSchemas
5
- from maleo_foundation.models.transfers.parameters.hash.sha256 import MaleoFoundationSHA256HashParametersTransfers
6
- from maleo_foundation.models.transfers.results.hash import MaleoFoundationHashResultsTransfers
5
+ from maleo_foundation.models.schemas.hash \
6
+ import MaleoFoundationHashSchemas
7
+ from maleo_foundation.models.transfers.parameters.hash.sha256 \
8
+ import MaleoFoundationSHA256HashParametersTransfers
9
+ from maleo_foundation.models.transfers.results.hash \
10
+ import MaleoFoundationHashResultsTransfers
7
11
  from maleo_foundation.utils.exceptions import BaseExceptions
8
12
 
9
13
  class MaleoFoundationSHA256HashClientService(ClientService):
10
- def hash(self, parameters:MaleoFoundationSHA256HashParametersTransfers.Hash) -> MaleoFoundationHashResultsTypes.Hash:
14
+ def hash(
15
+ self,
16
+ parameters:MaleoFoundationSHA256HashParametersTransfers.Hash
17
+ ) -> MaleoFoundationHashResultsTypes.Hash:
11
18
  """Generate a sha256 hash for the given message."""
12
19
  @BaseExceptions.service_exception_handler(
13
20
  operation="hashing single message",
@@ -21,7 +28,10 @@ class MaleoFoundationSHA256HashClientService(ClientService):
21
28
  return MaleoFoundationHashResultsTransfers.Hash(data=data)
22
29
  return _impl()
23
30
 
24
- def verify(self, parameters:MaleoFoundationSHA256HashParametersTransfers.Verify) -> MaleoFoundationHashResultsTypes.Verify:
31
+ def verify(
32
+ self,
33
+ parameters:MaleoFoundationSHA256HashParametersTransfers.Verify
34
+ ) -> MaleoFoundationHashResultsTypes.Verify:
25
35
  """Verify a message against the given message hash."""
26
36
  @BaseExceptions.service_exception_handler(
27
37
  operation="verify single hash",
@@ -1,15 +1,22 @@
1
1
  from cryptography.hazmat.backends import default_backend
2
2
  from cryptography.hazmat.primitives.asymmetric import rsa
3
3
  from cryptography.hazmat.primitives import serialization
4
- from maleo_foundation.expanded_types.key import MaleoFoundationKeyResultsTypes
4
+ from maleo_foundation.expanded_types.key \
5
+ import MaleoFoundationKeyResultsTypes
5
6
  from maleo_foundation.managers.client.base import ClientService
6
- from maleo_foundation.models.transfers.general.key import MaleoFoundationKeyGeneralTransfers
7
- from maleo_foundation.models.transfers.parameters.key import MaleoFoundationKeyParametersTransfers
8
- from maleo_foundation.models.transfers.results.key import MaleoFoundationKeyResultsTransfers
7
+ from maleo_foundation.models.transfers.general.key \
8
+ import MaleoFoundationKeyGeneralTransfers
9
+ from maleo_foundation.models.transfers.parameters.key \
10
+ import MaleoFoundationKeyParametersTransfers
11
+ from maleo_foundation.models.transfers.results.key \
12
+ import MaleoFoundationKeyResultsTransfers
9
13
  from maleo_foundation.utils.exceptions import BaseExceptions
10
14
 
11
15
  class MaleoFoundationKeyClientService(ClientService):
12
- def create_private(self, parameters:MaleoFoundationKeyParametersTransfers.CreatePrivateOrPair) -> MaleoFoundationKeyResultsTypes.CreatePrivate:
16
+ def create_private(
17
+ self,
18
+ parameters:MaleoFoundationKeyParametersTransfers.CreatePrivateOrPair
19
+ ) -> MaleoFoundationKeyResultsTypes.CreatePrivate:
13
20
  """Create an RSA private key with X.509 encoding in .pem format."""
14
21
  @BaseExceptions.service_exception_handler(
15
22
  operation="creating private key",
@@ -27,7 +34,9 @@ class MaleoFoundationKeyClientService(ClientService):
27
34
  if parameters.password is None:
28
35
  encryption_algorithm = serialization.NoEncryption()
29
36
  else:
30
- encryption_algorithm = serialization.BestAvailableEncryption(parameters.password.encode())
37
+ encryption_algorithm = serialization.BestAvailableEncryption(
38
+ parameters.password.encode()
39
+ )
31
40
 
32
41
  #* Serialize private key to PEM format
33
42
  private_key_bytes = private_key.private_bytes(
@@ -37,11 +46,16 @@ class MaleoFoundationKeyClientService(ClientService):
37
46
  )
38
47
 
39
48
  self._logger.info("Successfully created private key")
40
- data = MaleoFoundationKeyGeneralTransfers.PrivateKey(value=private_key_bytes.decode())
49
+ data = MaleoFoundationKeyGeneralTransfers.PrivateKey(
50
+ value=private_key_bytes.decode()
51
+ )
41
52
  return MaleoFoundationKeyResultsTransfers.CreatePrivate(data=data)
42
53
  return _impl()
43
54
 
44
- def create_public(self, parameters:MaleoFoundationKeyParametersTransfers.CreatePublic) -> MaleoFoundationKeyResultsTypes.CreatePublic:
55
+ def create_public(
56
+ self,
57
+ parameters:MaleoFoundationKeyParametersTransfers.CreatePublic
58
+ ) -> MaleoFoundationKeyResultsTypes.CreatePublic:
45
59
  """Create an RSA public key with X.509 encoding in .pem format."""
46
60
  @BaseExceptions.service_exception_handler(
47
61
  operation="creating public key",
@@ -66,7 +80,9 @@ class MaleoFoundationKeyClientService(ClientService):
66
80
  )
67
81
 
68
82
  self._logger.info("Successfully created public key")
69
- data = MaleoFoundationKeyGeneralTransfers.PublicKey(value=public_key_bytes.decode())
83
+ data = MaleoFoundationKeyGeneralTransfers.PublicKey(
84
+ value=public_key_bytes.decode()
85
+ )
70
86
  return MaleoFoundationKeyResultsTransfers.CreatePublic(data=data)
71
87
  return _impl()
72
88
 
@@ -88,7 +104,9 @@ class MaleoFoundationKeyClientService(ClientService):
88
104
  if parameters.password is None:
89
105
  encryption_algorithm = serialization.NoEncryption()
90
106
  else:
91
- encryption_algorithm = serialization.BestAvailableEncryption(parameters.password.encode())
107
+ encryption_algorithm = serialization.BestAvailableEncryption(
108
+ parameters.password.encode()
109
+ )
92
110
 
93
111
  #* Serialize private key to PEM format
94
112
  private_key_bytes = private_key.private_bytes(
@@ -96,7 +114,9 @@ class MaleoFoundationKeyClientService(ClientService):
96
114
  format=serialization.PrivateFormat.PKCS8,
97
115
  encryption_algorithm=encryption_algorithm
98
116
  )
99
- private = MaleoFoundationKeyGeneralTransfers.PrivateKey(value=private_key_bytes.decode())
117
+ private = MaleoFoundationKeyGeneralTransfers.PrivateKey(
118
+ value=private_key_bytes.decode()
119
+ )
100
120
 
101
121
  public_key = private_key.public_key() #* Create public key
102
122
 
@@ -105,7 +125,9 @@ class MaleoFoundationKeyClientService(ClientService):
105
125
  encoding=serialization.Encoding.PEM,
106
126
  format=serialization.PublicFormat.SubjectPublicKeyInfo
107
127
  )
108
- public = MaleoFoundationKeyGeneralTransfers.PublicKey(value=public_key_bytes.decode())
128
+ public = MaleoFoundationKeyGeneralTransfers.PublicKey(
129
+ value=public_key_bytes.decode()
130
+ )
109
131
 
110
132
  self._logger.info("Successfully created key pair")
111
133
  data = MaleoFoundationKeyGeneralTransfers.KeyPair(private=private, public=public)
@@ -2,16 +2,23 @@ from base64 import b64decode, b64encode
2
2
  from Crypto.Hash import SHA256
3
3
  from Crypto.Signature import pkcs1_15
4
4
  from maleo_foundation.enums import BaseEnums
5
- from maleo_foundation.expanded_types.signature import MaleoFoundationSignatureResultsTypes
5
+ from maleo_foundation.expanded_types.signature \
6
+ import MaleoFoundationSignatureResultsTypes
6
7
  from maleo_foundation.managers.client.base import ClientService
7
- from maleo_foundation.models.schemas.signature import MaleoFoundationSignatureSchemas
8
- from maleo_foundation.models.transfers.parameters.signature import MaleoFoundationSignatureParametersTransfers
9
- from maleo_foundation.models.transfers.results.signature import MaleoFoundationSignatureResultsTransfers
8
+ from maleo_foundation.models.schemas.signature \
9
+ import MaleoFoundationSignatureSchemas
10
+ from maleo_foundation.models.transfers.parameters.signature \
11
+ import MaleoFoundationSignatureParametersTransfers
12
+ from maleo_foundation.models.transfers.results.signature \
13
+ import MaleoFoundationSignatureResultsTransfers
10
14
  from maleo_foundation.utils.exceptions import BaseExceptions
11
15
  from maleo_foundation.utils.loaders.key.rsa import RSAKeyLoader
12
16
 
13
17
  class MaleoFoundationSignatureClientService(ClientService):
14
- def sign(self, parameters:MaleoFoundationSignatureParametersTransfers.Sign) -> MaleoFoundationSignatureResultsTypes.Sign:
18
+ def sign(
19
+ self,
20
+ parameters:MaleoFoundationSignatureParametersTransfers.Sign
21
+ ) -> MaleoFoundationSignatureResultsTypes.Sign:
15
22
  @BaseExceptions.service_exception_handler(
16
23
  operation="signing single message",
17
24
  logger=self._logger,
@@ -19,18 +26,30 @@ class MaleoFoundationSignatureClientService(ClientService):
19
26
  )
20
27
  def _impl():
21
28
  try:
22
- private_key = RSAKeyLoader.load_with_pycryptodome(type=BaseEnums.KeyType.PRIVATE, extern_key=parameters.key, passphrase=parameters.password)
29
+ private_key = RSAKeyLoader.load_with_pycryptodome(
30
+ type=BaseEnums.KeyType.PRIVATE,
31
+ extern_key=parameters.key,
32
+ passphrase=parameters.password
33
+ )
23
34
  except TypeError:
24
35
  message = "Invalid key type"
25
36
  description = "A private key must be used for signing a message"
26
37
  other = "Ensure the given key is of type private key"
27
- return MaleoFoundationSignatureResultsTransfers.Fail(message=message, description=description, other=other)
38
+ return MaleoFoundationSignatureResultsTransfers.Fail(
39
+ message=message,
40
+ description=description,
41
+ other=other
42
+ )
28
43
  except Exception as e:
29
44
  self._logger.error("Unexpected error occured while trying to import key:\n'%s'", str(e), exc_info=True)
30
45
  message = "Invalid key"
31
46
  description = "Unexpected error occured while trying to import key"
32
47
  other = "Ensure given key is valid"
33
- return MaleoFoundationSignatureResultsTransfers.Fail(message=message, description=description, other=other)
48
+ return MaleoFoundationSignatureResultsTransfers.Fail(
49
+ message=message,
50
+ description=description,
51
+ other=other
52
+ )
34
53
  hash = SHA256.new(parameters.message.encode()) #* Generate message hash
35
54
  signature = b64encode(pkcs1_15.new(private_key).sign(hash)).decode() #* Sign the hashed message
36
55
  data = MaleoFoundationSignatureSchemas.Signature(signature=signature)
@@ -38,7 +57,10 @@ class MaleoFoundationSignatureClientService(ClientService):
38
57
  return MaleoFoundationSignatureResultsTransfers.Sign(data=data)
39
58
  return _impl()
40
59
 
41
- def verify(self, parameters:MaleoFoundationSignatureParametersTransfers.Verify) -> MaleoFoundationSignatureResultsTypes.Verify:
60
+ def verify(
61
+ self,
62
+ parameters:MaleoFoundationSignatureParametersTransfers.Verify
63
+ ) -> MaleoFoundationSignatureResultsTypes.Verify:
42
64
  @BaseExceptions.service_exception_handler(
43
65
  operation="verify single signature",
44
66
  logger=self._logger,
@@ -46,18 +68,29 @@ class MaleoFoundationSignatureClientService(ClientService):
46
68
  )
47
69
  def _impl():
48
70
  try:
49
- public_key = RSAKeyLoader.load_with_pycryptodome(type=BaseEnums.KeyType.PUBLIC, extern_key=parameters.key)
71
+ public_key = RSAKeyLoader.load_with_pycryptodome(
72
+ type=BaseEnums.KeyType.PUBLIC,
73
+ extern_key=parameters.key
74
+ )
50
75
  except TypeError:
51
76
  message = "Invalid key type"
52
77
  description = "A public key must be used for verifying a signature"
53
78
  other = "Ensure the given key is of type public key"
54
- return MaleoFoundationSignatureResultsTransfers.Fail(message=message, description=description, other=other)
79
+ return MaleoFoundationSignatureResultsTransfers.Fail(
80
+ message=message,
81
+ description=description,
82
+ other=other
83
+ )
55
84
  except Exception as e:
56
85
  self._logger.error("Unexpected error occured while trying to import key:\n'%s'", str(e), exc_info=True)
57
86
  message = "Invalid key"
58
87
  description = "Unexpected error occured while trying to import key"
59
88
  other = "Ensure given key is valid"
60
- return MaleoFoundationSignatureResultsTransfers.Fail(message=message, description=description, other=other)
89
+ return MaleoFoundationSignatureResultsTransfers.Fail(
90
+ message=message,
91
+ description=description,
92
+ other=other
93
+ )
61
94
  hash = SHA256.new(parameters.message.encode()) #* Generate message hash
62
95
  #* Verify the hashed message and decoded signature
63
96
  try: