mirror of https://github.com/dirtbags/netarch.git
Refined XorMask
This commit is contained in:
parent
f52666a4c9
commit
9949fc600c
|
@ -87,7 +87,7 @@ def bin(i):
|
|||
s = ''.join(str(x) for x in r)
|
||||
return s
|
||||
|
||||
class bitvector:
|
||||
class BitVector:
|
||||
def __init__(self, i=0, length=None):
|
||||
if type(i) == type(''):
|
||||
self._val = 0
|
||||
|
|
14
crypto.py
14
crypto.py
|
@ -214,21 +214,23 @@ def adds(txt):
|
|||
|
||||
|
||||
class XorMask:
|
||||
def __init__(self, mask):
|
||||
def __init__(self, mask, stick=False):
|
||||
self.offset = 0
|
||||
if type(mask) == type(''):
|
||||
self._mask = tuple(ord(m) for m in mask)
|
||||
else:
|
||||
self._mask = tuple(mask)
|
||||
self.stick = stick
|
||||
|
||||
def mask(self, s, stick=False):
|
||||
def __call__(self, s):
|
||||
r = []
|
||||
offset = self.offset
|
||||
for c in s:
|
||||
o = ord(c)
|
||||
r.append(chr(o ^ self._mask[self.offset]))
|
||||
self.offset = (self.offset + 1) % len(self._mask)
|
||||
if not stick:
|
||||
self.offset = 0
|
||||
r.append(chr(o ^ self._mask[offset]))
|
||||
offset = (offset + 1) % len(self._mask)
|
||||
if self.stick:
|
||||
self.offset = offset
|
||||
return ''.join(r)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue