|
| 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( |
| 49 | + ["openssl", "genrsa", "-out", "rsa_private.pem", "2048"]) |
| 50 | +proc.wait() |
| 51 | +proc = subprocess.Popen( |
| 52 | + ["openssl", "rsa", "-in", "rsa_private.pem", |
| 53 | + "-pubout", "-out", "rsa_public.pem"] |
| 54 | +) |
| 55 | +proc.wait() |
| 56 | + |
| 57 | +# Open generated private key file |
| 58 | +try: |
| 59 | + with open("rsa_private.pem", "rb") as file: |
| 60 | + private_key = file.read() |
| 61 | +except: |
| 62 | + print("No file named rsa_private.pem found in directory.") |
| 63 | +pk = rsa.PrivateKey.load_pkcs1(private_key) |
| 64 | + |
| 65 | +print("Copy and paste this into your secrets.py file:\n") |
| 66 | +print("\"private_key\": " + str(pk)[10:] + ",") |
0 commit comments