示例#1
0
    def export_gcm_encrypted_private_key(self, password: str, salt: str,
                                         n: int) -> str:
        """
        This interface is used to export an AES algorithm encrypted private key with the mode of GCM.

        :param password: the secret pass phrase to generate the keys from.
        :param salt: A string to use for better protection from dictionary attacks.
                      This value does not need to be kept secret, but it should be randomly chosen for each derivation.
                      It is recommended to be at least 8 bytes long.
        :param n: CPU/memory cost parameter. It must be a power of 2 and less than 2**32
        :return: an gcm encrypted private key in the form of string.
        """
        r = 8
        p = 8
        dk_len = 64
        scrypt = Scrypt(n, r, p, dk_len)
        derivedkey = scrypt.generate_kd(password, salt)
        iv = derivedkey[0:12]
        derivedhalf2 = derivedkey[32:64]
        mac_tag, cipher_text = AESHandler.aes_gcm_encrypt_with_iv(
            self.__private_key,
            self.__address.b58encode().encode(), derivedhalf2, iv)
        encrypted_key = b2a_hex(cipher_text) + b2a_hex(mac_tag)
        encrypted_key_str = base64.b64encode(a2b_hex(encrypted_key))
        return encrypted_key_str.decode()
示例#2
0
 def test_aes_gcm_with_iv(self):
     key = b'Sixteen byte key'
     plain_text = b'Attack at dawn'
     hdr = b'To your eyes only'
     iv = Random.new().read(AES.block_size)
     mac, cipher_text = AESHandler.aes_gcm_encrypt_with_iv(plain_text, hdr, key, iv)
     decrypt_out = AESHandler.aes_gcm_decrypt_with_iv(cipher_text, hdr, mac, key, iv)
     self.assertEqual(plain_text, decrypt_out)
示例#3
0
 def export_gcm_encrypted_private_key(self, password: str, salt: str, n: int):
     r = 8
     p = 8
     dk_len = 64
     scrypt = Scrypt(n, r, p, dk_len)
     derivedkey = scrypt.generate_kd(password, salt)
     iv = derivedkey[0:12]
     derivedhalf2 = derivedkey[32:64]
     mac_tag, cipher_text = AESHandler.aes_gcm_encrypt_with_iv(self.__private_key,
                                                               self.__address.b58encode().encode(),
                                                               derivedhalf2,
                                                               iv)
     encrypted_key = b2a_hex(cipher_text) + b2a_hex(mac_tag)
     encrypted_key_str = base64.b64encode(a2b_hex(encrypted_key))
     return encrypted_key_str.decode()