2
2
import os
3
3
import pathlib
4
4
5
- from typing import Union
5
+ from typing import Union , Optional
6
6
7
7
from cryptography .hazmat .primitives import serialization
8
8
from cryptography .hazmat .primitives .asymmetric import (
9
9
ed25519 as ED25519 ,
10
10
)
11
11
12
12
import synlink .crypto .ed25519 as ed25519
13
- from synlink .crypto .typing import PrivateKey , KeyPair
14
-
15
-
16
-
17
- from typing import Optional
13
+ from synlink .crypto .typing import KeyPair
18
14
19
15
HOME_DIR : str = pathlib .Path .home ().__str__ ()
20
16
SSH_DEFAULT_DIRECTORY = os .path .join (HOME_DIR , ".ssh" )
21
17
18
+ __all__ = ["load_open_ssh_private_key" ]
19
+
22
20
def load_ssh_private_key (
23
- ssh_dir : Union [str , os .PathLike ] = SSH_DEFAULT_DIRECTORY ,
24
- key_name : str = "id_ed25519" ,
25
- password : Optional [str ] = None ,
21
+ file : Union [str , os .PathLike ] = SSH_DEFAULT_DIRECTORY ,
22
+ password : Optional [Union [str , bytes ]] = None ,
26
23
) -> KeyPair :
27
24
"""Load private key from OpenSSL custom encoding, and reconstruct
28
25
key pair.
@@ -39,12 +36,14 @@ def load_ssh_private_key(
39
36
ValueError: If keys are malformed or incompatible
40
37
NotImplemented: If other then ed25519
41
38
Example:
42
- >>> keypair = load_ssh_keys (key_name="id_ed25519 ")
39
+ >>> keypair = load_ssh_private_key (key_name="~/.ssh/synlink_ed25519 ")
43
40
"""
44
- file = os .path .join (ssh_dir , key_name )
41
+ if isinstance (password , str ):
42
+ password = password .encode ()
43
+
45
44
with open (
46
45
file ,
47
- "rb " ,
46
+ "r+b " ,
48
47
) as reader :
49
48
buffer = serialization .load_ssh_private_key (
50
49
reader .read (- 1 ),
0 commit comments