def test_simplecryptography(): key = os.urandom(32) c = SimpleCryptography(key) buf = os.urandom(100) enc = c.encrypt(buf) dec = c.decrypt(enc) assert buf == dec
def test_simplecryptography_time(): key = os.urandom(32) c = SimpleCryptography(key) buf = b"a" * 1024 * 1024 * 100 start = time.time() c.encrypt(buf) end = time.time() print("100M:", end - start)
def __init__(self, io: IO, encrypt_key: Any, nonce_or_iv: Any, total_origin_len: int): encrypt_key = padding_key(encrypt_key, self.BLOCK_SIZE * 2) nonce_or_iv = padding_key(nonce_or_iv, self.BLOCK_SIZE) super().__init__(io, encrypt_key, nonce_or_iv, total_origin_len) self._crypto = SimpleCryptography(self._encrypt_key)
class SimpleDecryptIO(DecryptIO): def __init__(self, io: IO, encrypt_key: Any, total_origin_len: int): encrypt_key = padding_key(encrypt_key, 32) super().__init__(io, encrypt_key, None, total_origin_len) self._crypto = SimpleCryptography(self._encrypt_key) def read(self, size: int = -1) -> Optional[bytes]: data = self._io.read(size) if not data: return b"" self._offset += len(data) return self._crypto.decrypt(data) def seekable(self) -> bool: return True
class SimpleDecryptIO(DecryptIO): def __init__(self, io: IO, encrypt_key: bytes, nonce: bytes, total_origin_len: int): super().__init__(io, encrypt_key, nonce, total_origin_len) self._crypto = SimpleCryptography(self._encrypt_key + self._nonce_or_iv) def read(self, size: int = -1) -> Optional[bytes]: data = self._io.read(size) if not data: return b"" self._offset += len(data) return self._crypto.decrypt(data) def seekable(self) -> bool: return True
def __init__(self, io: IO, encrypt_key: bytes, nonce: bytes, total_origin_len: int): super().__init__(io, encrypt_key, nonce, total_origin_len) self._crypto = SimpleCryptography(self._encrypt_key + self._nonce_or_iv)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._crypto = SimpleCryptography(self._encrypt_key + self._nonce_or_iv)
def __init__(self, io: IO, encrypt_key: Any, total_origin_len: int): encrypt_key = padding_key(encrypt_key, 32) super().__init__(io, encrypt_key, None, total_origin_len) self._crypto = SimpleCryptography(self._encrypt_key)