1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

Merge pull request #18499 from mariemC/dist/tools/key_encryption

dist/tools/suit: encrypt an existing key
This commit is contained in:
benpicco 2022-08-23 19:23:22 +02:00 committed by GitHub
commit b232deea9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

38
dist/tools/suit/password_protect_key.py vendored Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env python3
#
# Copyright (C) 2021_ML!PA Consulting GmbH
#
import sys
import os
from cryptography.hazmat.primitives.serialization import Encoding
from cryptography.hazmat.primitives.serialization import PrivateFormat
from cryptography.hazmat.primitives.serialization import BestAvailableEncryption
from cryptography.hazmat.primitives import serialization
def main():
if len(sys.argv) < 2:
print(f"usage: {os.path.basename(sys.argv[0])} <password> <key .pem file>")
sys.exit(1)
if len(sys.argv) > 1:
file = sys.argv[2]
with open(file, "rb") as key_file:
pk = serialization.load_pem_private_key(key_file.read(),
password=None,
)
pw = str.encode(sys.argv[1])
crypt = BestAvailableEncryption(pw)
pem = pk.private_bytes(encoding=Encoding.PEM,
format=PrivateFormat.PKCS8,
encryption_algorithm=crypt,
)
with open(file, "wb") as f:
f.write(pem)
if __name__ == '__main__':
main()