-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsa_keys_writer.py
77 lines (59 loc) · 2.51 KB
/
dsa_keys_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from .dsa_base import DSABase
class DSAKeysWriter(DSABase):
def __init__(self,
directory_path: str = None,
keys_name: str = None):
super().__init__()
self.directory_path = directory_path
self._keys_name = keys_name
def _get_keys_name(self) -> str:
return self._keys_name
def _set_keys_name(self, keys_name: str):
self._keys_name = keys_name
keys_name = property(_get_keys_name, _set_keys_name)
def _get_directory_path(self) -> str:
return self._directory_path
def _set_directory_path(self, directory_path: str):
self._directory_path = self._end_directory_path_with_slash(
directory_path)
directory_path = property(_get_directory_path, _set_directory_path)
@staticmethod
def _end_directory_path_with_slash(directory_path: str) -> str:
result = directory_path
if directory_path is not None and not directory_path.endswith('/'):
result += '/'
return result
@property
def public_key_path(self) -> str:
return "%s %s %s" % (
self._directory_path, self.keys_name, DSABase.PUBLIC_KEY_EXTENSION)
@property
def private_key_path(self) -> str:
return "%s %s %s" % (
self._directory_path, self.keys_name, DSABase.PRIVATE_KEY_EXTENSION)
def is_correct_keys_destination(self) -> bool:
return self.has_directory_path & self.has_keys_name \
& self.has_keys_container
def write_public_key(self):
if self.is_correct_keys_destination() \
& self.has_public_key:
self._write_all_values_from_public_key()
return self
def write_private_key(self):
if self.is_correct_keys_destination() and self.has_private_key:
with open(self.private_key_path, "wt") as file:
file.write(hex(self.private_key))
return self
@property
def has_directory_path(self) -> bool:
return self._directory_path is not None
@property
def has_keys_name(self) -> bool:
return self._keys_name is not None
def _write_all_values_from_public_key(self):
with open(self.public_key_path, "wt") as file:
public_key_as_array_of_int = self.public_key.to_list()
public_key_as_array_of_hex = [hex(value) for value in
public_key_as_array_of_int]
public_key_as_string = " ".join(public_key_as_array_of_hex)
file.write(public_key_as_string)