def test_rsa_provider_init_invalid_passphrase(self): private_key = RSA.generate(2048) public_key = private_key.publickey() passphrase = random_string(6) invalid_passphrase = random_string(8) with open('./rsa-test.private_key.pem', 'wb') as f: f.write(private_key.exportKey(passphrase=passphrase)) with open('./rsa-test.public_key.pem', 'wb') as f: f.write(public_key.exportKey(passphrase=passphrase)) self.assertRaises(ClientError, LocalRsaProvider, dir='./', key='rsa-test', passphrase=invalid_passphrase) silently_remove('./rsa-test.public_key.pem') silently_remove('./rsa-test.private_key.pem') private_key_str = RsaKey.exportKey(private_key, passphrase=passphrase) public_key_str = RsaKey.exportKey(public_key, passphrase=passphrase) self.assertRaises(ClientError, RsaProvider, key_pair={ 'private_key': private_key_str, 'public_key': public_key_str }, passphrase=invalid_passphrase)
def save_ssid_pk(self, ssid: str, ap_pk: RsaKey): """ Saves an SSID/public key pair to the station's memory Args: ssid (str): The SSID to save ap_pk (RsaKey): The public key associated with the ssid """ self.saved.add((ssid, ap_pk.exportKey()))
def save_private_key(key: RSA.RsaKey, password): filename = os.getcwd() + "/keys/private_key/private_key.txt" key, vector = encrypt_key(key.exportKey(), password) os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, "wb") as f: f.write(key) f.write(vector) f.close()
def store_key(key: RsaKey, friendly_name: str = None): if not isinstance(key, RsaKey): raise ValueError('First argument must be an RsaKey!') name = friendly_name if friendly_name else str(uuid.uuid4()) file_name = f'{paths.PATH_WALLETS}/{name}.der' os.makedirs(os.path.dirname(file_name), exist_ok=True) with open(file_name, 'wb+') as file: file.write(key.exportKey(format='DER'))
def get_http_authentication(private_key: RsaKey, private_key_id: str) -> HTTPSignatureHeaderAuth: """ Get HTTP signature authentication for a request. """ key = private_key.exportKey() return HTTPSignatureHeaderAuth( headers=["(request-target)", "user-agent", "host", "date"], algorithm="rsa-sha256", key=key, key_id=private_key_id, )
def _save_keypair(self, key: RSA.RsaKey) -> None: self._priv = key.exportKey("PEM") self._pub = key.publickey().exportKey("DER")
def parse_key(key: RSA.RsaKey) -> str: """Returns the string version of a RSA key""" return binascii.hexlify(key.exportKey( format='DER')).decode('ascii')
OSS_STS_KEY = os.getenv("OSS_TEST_STS_KEY") OSS_STS_ARN = os.getenv("OSS_TEST_STS_ARN") OSS_PAYER_UID = os.getenv("OSS_TEST_PAYER_UID") OSS_PAYER_ID = os.getenv("OSS_TEST_PAYER_ACCESS_KEY_ID") OSS_PAYER_SECRET = os.getenv("OSS_TEST_PAYER_ACCESS_KEY_SECRET") OSS_INVENTORY_BUCKET_DESTINATION_ARN = os.getenv("OSS_TEST_RAM_ROLE_ARN") OSS_INVENTORY_BUCKET_DESTINATION_ACCOUNT = os.getenv("OSS_TEST_RAM_UID") OSS_AUTH_VERSION = None OSS_TEST_AUTH_SERVER_HOST = os.getenv("OSS_TEST_AUTH_SERVER_HOST") private_key = RSA.generate(1024) public_key = private_key.publickey() private_key_str = RsaKey.exportKey(private_key) public_key_str = RsaKey.exportKey(public_key) key_pair = {'private_key': private_key_str, 'public_key': public_key_str} private_key_compact = '''-----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQCokfiAVXXf5ImFzKDw+XO/UByW6mse2QsIgz3ZwBtMNu59fR5z ttSx+8fB7vR4CN3bTztrP9A6bjoN0FFnhlQ3vNJC5MFO1PByrE/MNd5AAfSVba93 I6sx8NSk5MzUCA4NJzAUqYOEWGtGBcom6kEF6MmR1EKib1Id8hpooY5xaQIDAQAB AoGAOPUZgkNeEMinrw31U3b2JS5sepG6oDG2CKpPu8OtdZMaAkzEfVTJiVoJpP2Y nPZiADhFW3e0ZAnak9BPsSsySRaSNmR465cG9tbqpXFKh9Rp/sCPo4Jq2n65yood JBrnGr6/xhYvNa14sQ6xjjfSgRNBSXD1XXNF4kALwgZyCAECQQDV7t4bTx9FbEs5 36nAxPsPM6aACXaOkv6d9LXI7A0J8Zf42FeBV6RK0q7QG5iNNd1WJHSXIITUizVF 6aX5NnvFAkEAybeXNOwUvYtkgxF4s28s6gn11c5HZw4/a8vZm2tXXK/QfTQrJVXp VwxmSr0FAajWAlcYN/fGkX1pWA041CKFVQJAG08ozzekeEpAuByTIOaEXgZr5MBQ gBbHpgZNBl8Lsw9CJSQI15wGfv6yDiLXsH8FyC9TKs+d5Tv4Cvquk0efOQJAd9OC lCKFs48hdyaiz9yEDsc57PdrvRFepVdj/gpGzD14mVerJbOiOF6aSV19ot27u4on
def save_public_key(key: RSA.RsaKey): filename = os.getcwd() + "/keys/public_key/public_key.txt" os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, "wb") as f: f.write(key.exportKey()) f.close()
def get_private_key(self, keys: RsaKey, passphrase: str) -> str: private_key = keys.exportKey(passphrase=passphrase, pkcs=self._pub_key_crypto_standard, protection=self._protection) return private_key.decode()