def encrypt_text(self, txt): """ Encrypt the content according to BIP38 :param str wif: Unencrypted key """ if not self.unlocked(): raise WalletLocked aes = AESCipher(self.masterkey) return "{}${}".format(self.deriveChecksum(txt), aes.encrypt(txt))
def _get_encrypted_masterpassword(self): """ Obtain the encrypted masterkey .. note:: The encrypted masterkey is checksummed, so that we can figure out that a provided password is correct or not. The checksum is only 4 bytes long! """ if not self.unlocked(): raise WalletLocked aes = AESCipher(self.password) return "{}${}".format(self._derive_checksum(self.masterkey), aes.encrypt(self.masterkey))
class Testcases(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.aes = AESCipher("Foobar") def test_str(self): self.assertIsInstance(AESCipher.str_to_bytes("foobar"), bytes) self.assertIsInstance(AESCipher.str_to_bytes(b"foobar"), bytes) def test_key(self): self.assertEqual(base64.b64encode(self.aes.key), b"6BGBj4DZw8ItV3uoPWGWeI5VO7QIU1u0IQXN/3JqYKs=") def test_pad(self): self.assertEqual(base64.b64encode(self.aes._pad(b"123456")), b"MTIzNDU2GhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGho=") def test_unpad(self): self.assertEqual( self.aes._unpad( base64.b64decode( b"MTIzNDU2GhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGho=")), b"123456") def test_padding(self): for n in range(1, 64): name = ''.join( random.choice(string.ascii_lowercase) for _ in range(n)) self.assertEqual( self.aes._unpad(self.aes._pad(bytes(name, "utf-8"))), bytes(name, "utf-8")) def test_encdec(self): for n in range(1, 16): name = ''.join( random.choice(string.ascii_lowercase) for _ in range(64)) self.assertEqual(self.aes.decrypt(self.aes.encrypt(name)), name)