summaryrefslogtreecommitdiff
path: root/rsa/_compat.py
diff options
context:
space:
mode:
authorMichael Manganiello <adamantike@users.noreply.github.com>2017-01-16 08:38:08 -0300
committerSybren A. Stüvel <sybren@stuvel.eu>2017-01-16 12:38:08 +0100
commitd3727172cedb409613739be6af197c3b03cc163d (patch)
tree9d5ff18b9a197fa3ef460dc879f6eb1d4ed2359c /rsa/_compat.py
parent81f0e95dd17bbe99df2ac4958bd9511dd05c788f (diff)
downloadrsa-git-d3727172cedb409613739be6af197c3b03cc163d.tar.gz
Implementation of bitwise XOR function for bytes object (#72)
Diffstat (limited to 'rsa/_compat.py')
-rw-r--r--rsa/_compat.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/rsa/_compat.py b/rsa/_compat.py
index 1e51368..38bab08 100644
--- a/rsa/_compat.py
+++ b/rsa/_compat.py
@@ -99,6 +99,27 @@ def byte(num):
return pack("B", num)
+def xor_bytes(b1, b2):
+ """
+ Returns the bitwise XOR result between two bytes objects, b1 ^ b2.
+
+ Bitwise XOR operation is commutative, so order of parameters doesn't
+ generate different results. If parameters have different length, extra
+ length of the largest one is ignored.
+
+ :param b1:
+ First bytes object.
+ :param b2:
+ Second bytes object.
+ :returns:
+ Bytes object, result of XOR operation.
+ """
+ if PY2:
+ return ''.join(byte(ord(x) ^ ord(y)) for x, y in zip(b1, b2))
+
+ return bytes(x ^ y for x, y in zip(b1, b2))
+
+
def get_word_alignment(num, force_arch=64,
_machine_word_size=MACHINE_WORD_SIZE):
"""