def test_xor(self): self.assertEquals(xor(b'', b''), b'') self.assertEquals(xor(b'\x00', b'\x00'), b'\x00') self.assertEquals(xor(b'\x01', b'\x00'), b'\x01') self.assertEquals(xor(b'\x01\x01', b'\x00\x01'), b'\x01\x00') self.assertEquals(xor(b'banana', b'ananas'), b'\x03\x0f\x0f\x0f\x0f\x12')
def _unprotect(self, string): """ Base64 decode and XOR the given `string` with the next salsa. Returns an unprotected string. """ tmp = base64.b64decode(string.encode("utf-8")) return xor(tmp, self._get_salsa(len(tmp))).decode("utf-8")
def _protect(self, string): """ XORs the given `string` with the next salsa and base64 encodes it. Returns a protected string. """ tmp = bytes(xor(string, self._get_salsa(len(string)))) return base64.b64encode(tmp)
def _unprotect(self, string): """ Base64 decode and XOR the given `string` with the next salsa. Returns an unprotected string. """ tmp = base64.b64decode(string) return bytes(xor(tmp, self._get_salsa(len(tmp))))
def _protect(self, string): """ XORs the given `string` with the next salsa and base64 encodes it. Returns a protected string. """ encoded = string.encode("utf-8") tmp = xor(encoded, self._get_salsa(len(encoded))) return base64.b64encode(tmp).decode("utf-8")