Esempio n. 1
0
 def decrypt(self, password: str) -> None:
     assert self.encrypted, "Key is not encrypted."
     self.seed = aes_decrypt(password, self.seed)
     self.private_key = from_extended_key_string(
         self.ledger, aes_decrypt(password, self.private_key_string))
     self.password = password
     self.encrypted = False
Esempio n. 2
0
 def test_encrypt_iv_d(self, _):
     self.assertEqual(
         aes_encrypt(self.password, self.message),
         'ZmZmZmZmZmZmZmZmZmZmZjlrKptoKD+MFwDxcg3XtCD9qz8UWhEhq/TVJT5+Mtp2a8sE'
         'CaO6WQj7fYsWGu2Hvbc0qYqxdN0HeTsiO+cZRo3eJISgr3F+rXFYi5oSBlD2')
     self.assertTupleEqual(aes_decrypt(self.password, self.expected),
                           (self.message, b'f' * 16))
Esempio n. 3
0
 def _decrypt_private_key_string(self,
                                 password: str) -> Optional[PrivateKey]:
     if not self.private_key_string:
         return None
     private_key_string, self.init_vectors['private_key'] = aes_decrypt(
         password, self.private_key_string)
     if not private_key_string:
         return None
     return from_extended_key_string(self.ledger, private_key_string)
Esempio n. 4
0
 def decrypt(self, password: str) -> None:
     assert self.encrypted, "Key is not encrypted."
     try:
         seed, seed_iv = aes_decrypt(password, self.seed)
         pk_string, pk_iv = aes_decrypt(password, self.private_key_string)
     except ValueError:  # failed to remove padding, password is wrong
         return
     try:
         Mnemonic().mnemonic_decode(seed)
     except IndexError:  # failed to decode the seed, this either means it decrypted and is invalid
         # or that we hit an edge case where an incorrect password gave valid padding
         return
     try:
         private_key = from_extended_key_string(self.ledger, pk_string)
     except (TypeError, ValueError):
         return
     self.seed = seed
     self.seed_encryption_init_vector = seed_iv
     self.private_key = private_key
     self.private_key_encryption_init_vector = pk_iv
     self.password = password
     self.encrypted = False
Esempio n. 5
0
 def _decrypt_seed(self, password: str) -> str:
     if not self.seed:
         return ""
     seed, self.init_vectors['seed'] = aes_decrypt(password, self.seed)
     if not seed:
         return ""
     try:
         Mnemonic().mnemonic_decode(seed)
     except IndexError:
         # failed to decode the seed, this either means it decrypted and is invalid
         # or that we hit an edge case where an incorrect password gave valid padding
         raise ValueError("Failed to decode seed.")
     return seed
Esempio n. 6
0
 def test_encrypt_decrypt(self):
     self.assertEqual(
         aes_decrypt('bubblegum', aes_encrypt('bubblegum',
                                              self.message))[0],
         self.message)
Esempio n. 7
0
 def test_decrypt_error(self):
     with self.assertRaises(InvalidPasswordError):
         aes_decrypt('notbubblegum', aes_encrypt('bubblegum', self.message))