6
6
import json
7
7
from binascii import hexlify
8
8
from getpass import getpass
9
+ from typing import Optional
9
10
10
11
from cryptography .hazmat .primitives import serialization
11
12
from cryptojwt .jwk import JWK
12
13
from cryptojwt .jwk .ec import (ECKey , import_private_key_from_file ,
13
14
import_public_key_from_file )
15
+ from cryptojwt .jwk .hmac import SYMKey
14
16
from cryptojwt .jwk .rsa import (RSAKey , import_private_rsa_key_from_file ,
15
17
import_public_rsa_key_from_file )
16
18
from cryptojwt .jwx import key_from_jwk_dict
@@ -45,8 +47,15 @@ def pem2ec(filename: str, kid: str = None, private: bool = False, passphrase: st
45
47
return jwk
46
48
47
49
48
- def pem2jwk (filename : str , kid : str , private : bool = False ) -> bytes :
50
+ def bin2jwk (filename : str , kid : str ) -> bytes :
51
+ """Read raw key from filename and return JWK"""
52
+ with open (filename , 'rb' ) as file :
53
+ content = file .read ()
54
+ return SYMKey (kid = kid , key = content )
49
55
56
+
57
+ def pem2jwk (filename : str , kid : str , private : bool = False ) -> JWK :
58
+ """Read PEM from filename and return JWK"""
50
59
with open (filename , 'rt' ) as file :
51
60
content = file .readlines ()
52
61
header = content [0 ]
@@ -72,8 +81,8 @@ def pem2jwk(filename: str, kid: str, private: bool = False) -> bytes:
72
81
return jwk
73
82
74
83
75
- def jwk2pem (jwk : JWK , private : bool = False ) -> bytes :
76
- """Convert asymmetric key from JWK to PEM"""
84
+ def export_jwk (jwk : JWK , private : bool = False ) -> bytes :
85
+ """Export JWK as PEM/bin """
77
86
78
87
if jwk .kty == 'oct' :
79
88
return jwk .key
@@ -96,6 +105,27 @@ def jwk2pem(jwk: JWK, private: bool = False) -> bytes:
96
105
return serialized
97
106
98
107
108
+ def output_jwk (jwk : JWK , private : bool = False , filename : Optional [str ] = None ) -> None :
109
+ """Output JWK to file"""
110
+ serialized = jwk .serialize (private = private )
111
+ if filename is not None :
112
+ with open (filename , mode = 'wt' ) as file :
113
+ file .write (json .dumps (serialized ))
114
+ else :
115
+ print (json .dumps (serialized , indent = 4 ))
116
+
117
+
118
+ def output_bytes (data : bytes , binary : bool = False , filename : Optional [str ] = None ) -> None :
119
+ """Output data to file"""
120
+ if filename is not None :
121
+ with open (filename , mode = 'wb' ) as file :
122
+ file .write (data )
123
+ else :
124
+ if binary :
125
+ print (hexlify (data ).decode ())
126
+ else :
127
+ print (data .decode ())
128
+
99
129
def main ():
100
130
""" Main function"""
101
131
parser = argparse .ArgumentParser (description = 'JWK Conversion Utility' )
@@ -119,25 +149,14 @@ def main():
119
149
120
150
if f .endswith ('.json' ):
121
151
jwk = jwk_from_file (f , args .private )
122
- serialized = jwk2pem (jwk , args .private )
123
-
124
- if args .output :
125
- with open (args .output , mode = 'wt' ) as file :
126
- file .write (serialized )
127
- else :
128
- if jwk .kty == 'oct' :
129
- print (hexlify (serialized ).decode ())
130
- else :
131
- print (serialized .decode ())
152
+ serialized = export_jwk (jwk , args .private )
153
+ output_bytes (data = serialized , binary = (jwk .kty == 'oct' ), filename = args .output )
154
+ elif f .endswith ('.bin' ):
155
+ jwk = bin2jwk (f , args .kid )
156
+ output_jwk (jwk = jwk , private = True , filename = args .output )
132
157
elif f .endswith ('.pem' ):
133
158
jwk = pem2jwk (f , args .kid , args .private )
134
- serialized = jwk .serialize (private = args .private )
135
-
136
- if args .output :
137
- with open (args .output , mode = 'wt' ) as file :
138
- file .write (json .dumps (serialized ))
139
- else :
140
- print (json .dumps (serialized , indent = 4 ))
159
+ output_jwk (jwk = jwk , private = args .private , filename = args .output )
141
160
else :
142
161
exit (- 1 )
143
162
0 commit comments