Mon, 07 Nov 2022 17:19:58 +0100
Corrected/acknowledged some bad import style and removed some obsolete code.
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
3 | # Copyright (c) 2018 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing the threat list info class. |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
10 | |
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
11 | class ThreatList: |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
12 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13 | Class implementing the threat list info. |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
15 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
16 | def __init__(self, threatType, platformType, threatEntryType): |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18 | Constructor |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
19 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20 | @param threatType threat type |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
21 | @type str |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | @param platformType platform type |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | @type str |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
24 | @param threatEntryType threat entry type |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | @type str |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | self.threatType = threatType |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | self.platformType = platformType |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | self.threatEntryType = threatEntryType |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | @classmethod |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | def fromApiEntry(cls, entry): |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
34 | Class method to instantiate a threat list given a threat list entry |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | dictionary. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
36 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | @param entry threat list entry dictionary |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | @type dict |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | @return instantiated object |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | @rtype ThreatList |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
42 | return cls(entry["threatType"], entry["platformType"], entry["threatEntryType"]) |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | def asTuple(self): |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | Public method to convert the object to a tuple. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
47 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | @return tuple containing the threat list info |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49 | @rtype tuple of (str, str, str) |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
50 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | return (self.threatType, self.platformType, self.threatEntryType) |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52 | |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
53 | def __repr__(self): |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
54 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
55 | Special method to generate a printable representation. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
56 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | @return printable representation |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
58 | @rtype str |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
59 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
60 | return "/".join(self.asTuple()) |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61 | |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62 | |
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
63 | class HashPrefixList: |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
64 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
65 | Class implementing a container for threat list data. |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
67 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
68 | def __init__(self, prefixLength, rawHashes): |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
70 | Constructor |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
71 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | @param prefixLength length of each hash prefix |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
73 | @type int |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
74 | @param rawHashes raw hash prefixes of given length concatenated and |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
75 | sorted in lexicographical order |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
76 | @type str |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
77 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
78 | self.__prefixLength = prefixLength |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
79 | self.__rawHashes = rawHashes |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
80 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
81 | def __len__(self): |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83 | Special method to calculate the number of entries. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
84 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
85 | @return length |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86 | @rtype int |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
87 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88 | return len(self.__rawHashes) // self.__prefixLength |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
89 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
90 | def __iter__(self): |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
91 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
92 | Special method to iterate over the raw hashes. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
93 | |
6229
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
94 | @return iterator object |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
95 | @rtype iterator |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
96 | """ |
72b40274a3b6
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
97 | n = self.__prefixLength |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
98 | return ( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
99 | self.__rawHashes[index : index + n] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
100 | for index in range(0, len(self.__rawHashes), n) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
101 | ) |