Created
July 4, 2020 23:26
-
-
Save thanakijwanavit/24b4e842269b8321c536d50fd0500a00 to your computer and use it in GitHub Desktop.
create a new rsa key and save it to aws secretstore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from Crypto.PublicKey import RSA | |
| def newKey(phoneHash:str): | |
| '''generat new rsa key and store in database''' | |
| if len(list(PynamoSecret.query(phoneHash))) > 0 : | |
| print('hash key exists, throwing error') | |
| raise ValueError('secret key exists') | |
| else: | |
| print('there is no secret key, generating one') | |
| private_key, public_key = generate_RSA() | |
| response = secret.create_secret( | |
| Name = phoneHash, | |
| Description = 'rsa key for user encryption', | |
| SecretString = json.dumps({ | |
| 'private_key' : private_key, | |
| 'public_key' : public_key | |
| }) | |
| ) | |
| pynamoSecret = PynamoSecret.fromSecretResponse( phoneHash, response) | |
| print(pynamoSecret.save()) | |
| print(private_key, public_key) | |
| return RSA.import_key(private_key) | |
| def generate_RSA(bits=2048): | |
| new_key = RSA.generate(bits, e=65537) | |
| public_key = new_key.publickey().exportKey("PEM").decode() | |
| private_key = new_key.exportKey("PEM").decode() | |
| return private_key, public_key | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment