Skip to content

Commit e2e02fb

Browse files
committed
simplify
1 parent 2d4d330 commit e2e02fb

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

src/cryptojwt/tools/keyconv.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
import json
77
from binascii import hexlify
88
from getpass import getpass
9+
from typing import Optional
910

1011
from cryptography.hazmat.primitives import serialization
1112
from cryptojwt.jwk import JWK
1213
from cryptojwt.jwk.ec import (ECKey, import_private_key_from_file,
1314
import_public_key_from_file)
15+
from cryptojwt.jwk.hmac import SYMKey
1416
from cryptojwt.jwk.rsa import (RSAKey, import_private_rsa_key_from_file,
1517
import_public_rsa_key_from_file)
1618
from cryptojwt.jwx import key_from_jwk_dict
@@ -45,8 +47,15 @@ def pem2ec(filename: str, kid: str = None, private: bool = False, passphrase: st
4547
return jwk
4648

4749

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)
4955

56+
57+
def pem2jwk(filename: str, kid: str, private: bool = False) -> JWK:
58+
"""Read PEM from filename and return JWK"""
5059
with open(filename, 'rt') as file:
5160
content = file.readlines()
5261
header = content[0]
@@ -72,8 +81,8 @@ def pem2jwk(filename: str, kid: str, private: bool = False) -> bytes:
7281
return jwk
7382

7483

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"""
7786

7887
if jwk.kty == 'oct':
7988
return jwk.key
@@ -96,6 +105,27 @@ def jwk2pem(jwk: JWK, private: bool = False) -> bytes:
96105
return serialized
97106

98107

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+
99129
def main():
100130
""" Main function"""
101131
parser = argparse.ArgumentParser(description='JWK Conversion Utility')
@@ -119,25 +149,14 @@ def main():
119149

120150
if f.endswith('.json'):
121151
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)
132157
elif f.endswith('.pem'):
133158
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)
141160
else:
142161
exit(-1)
143162

0 commit comments

Comments
 (0)