Projekt

Obecné

Profil

Stáhnout (2.97 KB) Statistiky
| Větev: | Tag: | Revize:
1 151e7604 Jan Pašek
from injector import inject
2
3 f62119d4 Stanislav Král
from src.dao.private_key_repository import PrivateKeyRepository
4
from src.model.private_key import PrivateKey
5
from src.services.cryptography import CryptographyService
6
7
8
class KeyService:
9
10 151e7604 Jan Pašek
    @inject
11 f62119d4 Stanislav Král
    def __init__(self, cryptography_service: CryptographyService, private_key_repository: PrivateKeyRepository):
12
        self.cryptography_service = cryptography_service
13
        self.private_key_repository = private_key_repository
14
15 a6727aa9 Stanislav Král
    def create_new_key(self, passphrase="") -> PrivateKey:
16
        """
17
        Creates a new private key using the given passphrase.
18
        :param passphrase: Passphrase to be used when encrypting the PK
19
        :return: An instance of the <PrivateKey> class representing the generated PK
20
        """
21 f62119d4 Stanislav Král
        # generate a new private key
22
        private_key_pem = self.cryptography_service.create_private_key(passphrase)
23
24
        # store generated PK and the passphrase in a wrapper
25
        private_key = PrivateKey(-1, private_key_pem, passphrase)
26
27
        # store the wrapper in the PK repository
28
        private_key_id = self.private_key_repository.create(private_key)
29
30
        # assign the generated ID to the wrapper
31
        private_key.private_key_id = private_key_id
32
33
        return private_key
34
35
    def get_key(self, unique_id):
36 a6727aa9 Stanislav Král
        """
37
        Tries to fetch a PK using the given ID.
38
        :param unique_id: ID of the PK to be found
39
        :return:An instance of the required PK or `None`
40
        """
41 f62119d4 Stanislav Král
        return self.private_key_repository.read(unique_id)
42
43 699a4f25 Stanislav Král
    def get_keys(self, unique_ids=None):
44 a6727aa9 Stanislav Král
        """
45
        Tries to fetch all PKs in the repository. Exact PKs to be fetched can be specified using the `unique_ids`
46
        parameter. If `unique_ids` parameter is not passed then all PKs in the repository are returned.
47
        :param unique_ids: An array containing IDs of PKs to be fetched from the repository.
48
        :return: A list of instances of the PrivateKey class representing the PKs found
49
        """
50 699a4f25 Stanislav Král
        if unique_ids is None:
51
            return self.private_key_repository.read_all()
52
        else:
53
            # TODO this is very inefficient
54
            return [self.private_key_repository.read(identifier) for identifier in unique_ids]
55 f62119d4 Stanislav Král
56
    def delete_key(self, unique_id):
57 a6727aa9 Stanislav Král
        """
58
        Deletes a private key
59
60
        :param unique_id: ID of specific certificate to be deleted
61
        :return: `True` when the deletion was successful. `False` in other case
62
        """
63 f62119d4 Stanislav Král
        return self.private_key_repository.delete(unique_id)
64 19d25d2f Stanislav Král
65
    def get_public_key(self, private_key: PrivateKey):
66
        """
67
        Extracts a public key from the given private key
68
        :param private_key: private key from which a public key should be extracted
69
        :return: a string containing the extracted public key in PEM format
70
        """
71 6abfb037 Stanislav Král
        return self.cryptography_service.extract_public_key_from_private_key(private_key.private_key,
72
                                                                             private_key.password)