-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.py
112 lines (92 loc) · 3.27 KB
/
state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from math import log, sqrt
from collections import defaultdict
class SpritzState:
def __init__(self, state=None, registers=None, permutation=None, string=None):
if state:
registers = state[0]
permutation = state[1]
elif string:
# TODO: not very safe
state = eval(string)
registers = state[0]
permutation = state[1]
elif registers is None or permutation is None:
raise TypeError(
'expected either registers and permutation or string'
)
# format of registers is list, [i, j, k, z, a, w], same as in cipher
# implementation class
self.i, self.j, self.k, self.z = registers[:4]
self.w = registers[-1]
self.S = permutation[:]
self.free = set(range(len(permutation))) - set(permutation)
def swap(self):
self.S[self.i], self.S[self.j] = self.S[self.j], self.S[self.i]
# conversion from variables (stored in class) to list (used in cipher
# class) and vice versa
@property
def registers(self):
return [self.i, self.j, self.k, self.z, 0, self.w]
@registers.setter
def registers(self, value):
self.i, self.j, self.k, self.z = value[:4]
self.w = value[-1]
@property
def state(self):
return [[self.i, self.j, self.k, self.z, 0, self.w], self.S]
@property
def size(self):
return len(self.S)
def __deepcopy__(self, memo):
return SpritzState([self.registers, self.S])
def __eq__(self, other):
return self.registers == other.registers and self.S == other.S
def __str__(self):
return '[{}, {}]'.format(
str(self.registers),
str(self.S)
)
class RC4State:
def __init__(self, state=None, registers=None, permutation=None, string=None):
if state:
registers = state[0]
permutation = state[1]
elif string:
# TODO: not very safe
state = eval(string)
registers = state[0]
permutation = state[1]
elif registers is None or permutation is None:
raise TypeError(
'expected either registers and permutation or string'
)
# format of registers is list, [i, j, k, z, a, w], same as in cipher
# implementation class
self.i, self.j = registers
self.S = permutation[:]
self.free = set(range(len(permutation))) - set(permutation)
def swap(self):
self.S[self.i], self.S[self.j] = self.S[self.j], self.S[self.i]
# conversion from variables (stored in class) to list (used in cipher
# class) and vice versa
@property
def registers(self):
return [self.i, self.j]
@registers.setter
def registers(self, value):
self.i, self.j = value
@property
def state(self):
return [[self.i, self.j], self.S]
@property
def size(self):
return len(self.S)
def __deepcopy__(self, memo):
return RC4State([self.registers, self.S])
def __eq__(self, other):
return self.registers == other.registers and self.S == other.S
def __str__(self):
return '[{}, {}]'.format(
str(self.registers),
str(self.S)
)