Projekt

Obecné

Profil

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