From 656e8bd7503ba4feb3aff3292c3850a9ec2064ba Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Fri, 26 Jun 2020 15:59:11 +0200 Subject: [PATCH] added xorCorrelator-Test --- __pycache__/xorCorrelator.cpython-38.pyc | Bin 0 -> 1464 bytes xorCorrelator.py | 41 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 __pycache__/xorCorrelator.cpython-38.pyc create mode 100644 xorCorrelator.py diff --git a/__pycache__/xorCorrelator.cpython-38.pyc b/__pycache__/xorCorrelator.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..20f9312e21bf9f3ce6b05cddf487960b526b3cfc GIT binary patch literal 1464 zcmcgsOK%e~5cb%+n@##4l=6}g6u6K}!wrP03R>WVBBTm|A_3a2O$puX2HQn#wR<6z z;L4ptd(2<*l~aEJ7cMa4G_*VnW$>(IPp-EpVIL2$p?hLqLQfYzlqBf<);(SpI>1fLB~%K6|kh zW4j^TN9!To5_EPOD~Viqr1A~>It7e1S(8i7pu${2*kC1lK3~B{R>DQRitrx$#NP&M zY{WDrgxT`Nn+|%KyuIq%t@i7puiCPZ#df=+wr!dfTeAIF7jIN*O*>0=6TM~H+l5{& zv{qSS3%xklagC*{XKY&Z2g?!VKzv7xv)%J(y10}TX_A>`6ed=!z==%duKr*9>%lH6*y7VyQIa@}o`W*uqSBpsG>6zM7apr;XB z&8So>YuuDAHdX%86XRd%qGT{oxg6D9T1tUB0r`t!Ig`D-_rXO>BBAC;C0< zgvwK2=P`InqjWueR_GZ0ed92gMpgr8;0{otQ^TL8L0?9z|HdsbI_8#Gj?3szZZ-A0 zo+r3KKy7{EZFQ;ARsTHt`tRpmL94^N2qSPJ-|GJ|Vl-~ld%lf`Zi)XLd(=!gpERR5 zp`!DP%Va9o(Au0s@KZTk;qdQ+WCt*w#}@dWXmNcOZ8RBbYVhBlqv;>6h5LB8F5{?3 zUu^T&83R2_ajyOu%r%Z(Z%|Cim4UvF*y5d|gpPheFOa%6&Tj@~-;2H-G?C^+FSp+J dM9#h#zQ^LFej&FqwM-luYPFa*!@1^M^9RJRC5-?8 literal 0 HcmV?d00001 diff --git a/xorCorrelator.py b/xorCorrelator.py new file mode 100644 index 0000000..0270bed --- /dev/null +++ b/xorCorrelator.py @@ -0,0 +1,41 @@ +import secrets + +def xor(a: bytes, b: bytes): + if len(a)!=len(b): + raise Exception("Bad Length") + return bytes([a[i]^b[i] for i in range(len(a))]) + +def cbcXor(blocks, key: bytes, iv: bytes = None, BS=16): + if len(key)!=BS: + raise Exception("Bad key len") + if not iv: + iv = secrets.token_bytes(16) + lastBlock = iv + res = [] + for block in blocks: + if len(block)!=BS: + raise Exception("Bad block length") + mid = xor(block, lastBlock) + enc = xor(mid, key) + lastBlock = enc + res.append(enc) + return res + +def decCbcXor(blocks, key: bytes, iv: bytes = None, BS=16): + if len(key)!=BS: + raise Exception("Bad key len") + if not iv: + iv = secrets.token_bytes(16) + lastBlock = iv + res = [] + for block in blocks: + if len(block)!=BS: + raise Exception("Bad block length") + mid = xor(block, key) + enc = xor(mid, lastBlock) + lastBlock = block + res.append(enc) + return res + +def randBlocks(num: int = 64): + return [secrets.token_bytes(16) for i in range(num)]