MCPcopy Create free account
hub / github.com/secdev/scapy / RandomEnumeration

Class RandomEnumeration

scapy/volatile.py:43–96  ·  view source on GitHub ↗

iterate through a sequence in random order. When all the values have been drawn, if forever=1, the drawing is done again. # noqa: E501 If renewkeys=0, the draw will be in the same order, guaranteeing that the same # noqa: E501 number will be drawn in not less than the number o

Source from the content-addressed store, hash-verified

41
42
43class RandomEnumeration:
44 """iterate through a sequence in random order.
45 When all the values have been drawn, if forever=1, the drawing is done again. # noqa: E501
46 If renewkeys=0, the draw will be in the same order, guaranteeing that the same # noqa: E501
47 number will be drawn in not less than the number of integers of the sequence""" # noqa: E501
48
49 def __init__(self, inf, sup, seed=None, forever=1, renewkeys=0):
50 # type: (int, int, Optional[int], int, int) -> None
51 self.forever = forever
52 self.renewkeys = renewkeys
53 self.inf = inf
54 self.rnd = random.Random(seed)
55 self.sbox_size = 256
56
57 self.top = sup - inf + 1
58
59 n = 0
60 while (1 << n) < self.top:
61 n += 1
62 self.n = n
63
64 self.fs = min(3, (n + 1) // 2)
65 self.fsmask = 2**self.fs - 1
66 self.rounds = max(self.n, 3)
67 self.turns = 0
68 self.i = 0
69
70 def __iter__(self):
71 # type: () -> RandomEnumeration
72 return self
73
74 def next(self):
75 # type: () -> int
76 while True:
77 if self.turns == 0 or (self.i == 0 and self.renewkeys):
78 self.cnt_key = self.rnd.randint(0, 2**self.n - 1)
79 self.sbox = [self.rnd.randint(0, self.fsmask)
80 for _ in range(self.sbox_size)]
81 self.turns += 1
82 while self.i < 2**self.n:
83 ct = self.i ^ self.cnt_key
84 self.i += 1
85 for _ in range(self.rounds): # Unbalanced Feistel Network
86 lsb = ct & self.fsmask
87 ct >>= self.fs
88 lsb ^= self.sbox[ct % self.sbox_size]
89 ct |= lsb << (self.n - self.fs)
90
91 if ct < self.top:
92 return self.inf + ct
93 self.i = 0
94 if not self.forever:
95 raise StopIteration
96 __next__ = next
97
98
99_T = TypeVar('_T')

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…