decode_priv_key.py
1 # Copyright 2019 Google Inc. 2 # 3 # Modified by Brent Rubell for Adafruit Industries, 2019 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 # Copyright 2019 Google Inc. 17 # 18 # Licensed under the Apache License, Version 2.0 (the "License"); 19 # you may not use this file except in compliance with the License. 20 # You may obtain a copy of the License at 21 # 22 # http://www.apache.org/licenses/LICENSE-2.0 23 # 24 # Unless required by applicable law or agreed to in writing, software 25 # distributed under the License is distributed on an "AS IS" BASIS, 26 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27 # See the License for the specific language governing permissions and 28 # limitations under the License. 29 # 30 """ 31 `decode_priv_key.py` 32 =================================================================== 33 34 Generates RSA keys and decodes them using python-rsa 35 for use with a CircuitPython secrets file. 36 37 This script is designed to run on a computer, 38 NOT a CircuitPython device. 39 40 Requires Python-RSA (https://github.com/sybrenstuvel/python-rsa) 41 42 * Author(s): Google Inc., Brent Rubell 43 """ 44 import subprocess 45 import rsa 46 47 # Generate private and public RSA keys 48 proc = subprocess.Popen(["openssl", "genrsa", "-out", "rsa_private.pem", "2048"]) 49 proc.wait() 50 proc = subprocess.Popen( 51 ["openssl", "rsa", "-in", "rsa_private.pem", "-pubout", "-out", "rsa_public.pem"] 52 ) 53 proc.wait() 54 55 # Open generated private key file 56 try: 57 with open("rsa_private.pem", "rb") as file: 58 private_key = file.read() 59 except: 60 print("No file named rsa_private.pem found in directory.") 61 pk = rsa.PrivateKey.load_pkcs1(private_key) 62 63 print("Copy and paste this into your secrets.py file:\n") 64 print('"private_key": ' + str(pk)[10:] + ",")