Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Created July 4, 2020 23:26
Show Gist options
  • Save thanakijwanavit/24b4e842269b8321c536d50fd0500a00 to your computer and use it in GitHub Desktop.
Save thanakijwanavit/24b4e842269b8321c536d50fd0500a00 to your computer and use it in GitHub Desktop.
create a new rsa key and save it to aws secretstore
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