Browse Source

Rewrote seeding, enforce typechecking for seeds

Stefan Schmidt 5 years ago
parent
commit
bff4e39933
2 changed files with 6 additions and 14 deletions
  1. 5 13
      code/Attack/BaseAttack.py
  2. 1 1
      code/CLI.py

+ 5 - 13
code/Attack/BaseAttack.py

@@ -305,23 +305,15 @@ class BaseAttack(metaclass=abc.ABCMeta):
     #########################################
 
     @staticmethod
-    def set_seed(seed):
+    def set_seed(seed: int) -> None:
         """
         :param seed: The random seed to be set.
         """
-        seed_final = None
-        if isinstance(seed, int):
-            seed_final = seed
-        elif isinstance(seed, str):
-            if seed.isdigit():
-                seed_final = int(seed)
-            else:
-                hashed_seed = hashlib.sha1(seed.encode()).digest()
-                seed_final = int.from_bytes(hashed_seed, byteorder="little")
+        if not isinstance(seed, int):  # pragma: no cover
+            raise TypeError("Seed needs to be an integer")
 
-        if seed_final:
-            random.seed(seed_final)
-            np.random.seed(seed_final & 0xFFFFFFFF)
+        random.seed(seed)
+        np.random.seed(seed & 0xFFFFFFFF)
 
     def set_start_time(self) -> None:
         """

+ 1 - 1
code/CLI.py

@@ -67,7 +67,7 @@ class CLI(object):
                                  'in interval-wise, TCP checksum, and checking payload availability.',
                             action='store_true')
         parser.add_argument('-S', '--rngSeed', action='append', help='sets rng seed for testing or benchmarking',
-                            nargs='+', default=[])
+                            nargs='+', default=[], type=int)
         parser.add_argument('-T', '--time', help='measures packet generation time', action='store_true', default=False)
         parser.add_argument('-V', '--non-verbose', help='reduces terminal clutter', action='store_true', default=False)
         parser.add_argument('-o', '--output', metavar="PCAP_FILE", help='path to the output pcap file')