[feat] gen key

This commit is contained in:
SouthFox 2023-07-11 22:11:51 +08:00
parent f902c144a5
commit 54db6c3065
3 changed files with 40 additions and 0 deletions

11
data/.gitignore vendored Normal file
View file

@ -0,0 +1,11 @@
.DS_Store
.idea
*.log
tmp/
*.py[cod]
*.egg
build
htmlcov
*.pem

View file

@ -1,4 +1,8 @@
"Store App settings" "Store App settings"
from pathlib import Path
_ROOT_DIR = Path().parent.resolve()
KEY_PATH = _ROOT_DIR / "data" / "key.pem"
USER_AGENT = "COSCUP-demo" USER_AGENT = "COSCUP-demo"
AP_CONTENT_TYPE = "application/activity+json" AP_CONTENT_TYPE = "application/activity+json"

25
demo/utils/key.py Normal file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env python3
"""process Key."""
import sys
from pathlib import Path
from Crypto.PublicKey import RSA
from demo.config import KEY_PATH
def get_pubkey_as_pem(key_path: Path) -> str:
"""Exporting public key from private key file."""
text = key_path.read_text()
return RSA.import_key(text).public_key().export_key("PEM").decode("utf-8")
def gen_key():
"""Generate key."""
if KEY_PATH.exists():
print("is existing!")
sys.exit(2)
else:
k = RSA.generate(2048)
privkey_pem = k.exportKey("PEM").decode("utf-8")
KEY_PATH.write_text(privkey_pem)