-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate_keys.py
More file actions
48 lines (35 loc) 路 1.47 KB
/
Copy pathgenerate_keys.py
File metadata and controls
48 lines (35 loc) 路 1.47 KB
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
from pathlib import Path
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
def generate_keys() -> None:
"""Generate local demo keys for the credential walkthrough.
These keys are for local learning use only. They are written into an ignored
directory so the course repo does not model committed signing material.
"""
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend(),
)
pem_private = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
pem_public = private_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
output_dir = Path("local-dev-keys")
output_dir.mkdir(exist_ok=True)
private_key_path = output_dir / "private_key.pem"
public_key_path = output_dir / "public_key.pem"
private_key_path.write_bytes(pem_private)
public_key_path.write_bytes(pem_public)
print("Keys generated for local demo use:")
print(f" - {private_key_path}")
print(f" - {public_key_path}")
print("Do not commit these files or use them as a production trust anchor.")
if __name__ == "__main__":
generate_keys()