-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedraw2pem
executable file
·50 lines (43 loc) · 1.59 KB
/
edraw2pem
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
#!/usr/bin/python3
import argparse
import base64
import binascii
import sys
from cryptography.hazmat.primitives.asymmetric import ed448, ed25519
from cryptography.hazmat.primitives.serialization import (
Encoding,
NoEncryption,
PrivateFormat,
PublicFormat,
)
ap = argparse.ArgumentParser()
ap.add_argument("key", help="raw key (default: base64, Ed25519 public key)")
ap.add_argument("-x", "--hex", action="store_true", help="hex input instead of base64")
ap.add_argument("--ed25519priv", action="store_true", help="Ed448 private key")
ap.add_argument("--ed448pub", action="store_true", help="Ed448 public key")
ap.add_argument("--ed448priv", action="store_true", help="Ed448 private key")
args = ap.parse_args()
if args.ed448priv or args.ed448pub:
LEN = 57
else:
LEN = 32
if args.hex:
binx = binascii.unhexlify(args.key)
else:
binx = base64.b64decode(args.key)
if len(binx) != LEN:
print(f"{len(binx)}b wrong length ({LEN}b expected)")
sys.exit(1)
if args.ed448priv:
key = ed448.Ed448PrivateKey.from_private_bytes(binx)
pem = key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())
elif args.ed448pub:
key = ed448.Ed448PublicKey.from_public_bytes(binx)
pem = key.public_bytes(Encoding.PEM, PublicFormat.SubjectPublicKeyInfo)
elif args.ed25519priv:
key = ed25519.Ed25519PrivateKey.from_private_bytes(binx)
pem = key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())
else:
key = ed25519.Ed25519PublicKey.from_public_bytes(binx)
pem = key.public_bytes(Encoding.PEM, PublicFormat.SubjectPublicKeyInfo)
print(pem.decode(), end="")