Tue, 10 Apr 2018 19:39:13 +0200
Web Browser NG, Safe Browsing: refactored the code to prepare the implementation of the Lookup API (v4)
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 | |
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
|
3 | # Copyright (c) 2018 Detlev Offenbach <detlev@die-offenbachs.de> |
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 | from __future__ import unicode_literals, division |
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
|
11 | |
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 ThreatList(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
|
14 | """ |
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
|
15 | 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
|
16 | """ |
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 | 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
|
18 | """ |
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
|
19 | Constructor |
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 | |
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 | @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
|
22 | @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
|
23 | @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
|
24 | @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
|
25 | @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
|
26 | @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
|
27 | """ |
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.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
|
29 | 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
|
30 | 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
|
31 | |
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 | @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
|
33 | 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
|
34 | """ |
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 | 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
|
36 | 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
|
37 | |
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 | @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
|
39 | @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
|
40 | @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
|
41 | @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
|
42 | """ |
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 | return cls(entry['threatType'], entry['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
|
44 | entry['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
|
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 | 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
|
47 | """ |
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 | Public method to convert the object to a tuple. |
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 | |
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 | @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
|
51 | @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
|
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 | 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
|
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 | 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
|
56 | """ |
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 | Special method to generate a 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 | |
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 | @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
|
60 | @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
|
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 | return '/'.join(self.asTuple()) |
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
|
63 | |
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 HashPrefixList(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
|
66 | """ |
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
|
67 | 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
|
68 | """ |
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 | 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
|
70 | """ |
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
|
71 | Constructor |
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 | |
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 | @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
|
74 | @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
|
75 | @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
|
76 | 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
|
77 | @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
|
78 | """ |
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.__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
|
80 | self.__rawHashes = 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
|
81 | |
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 | 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
|
83 | """ |
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
|
84 | Special method to calculate the number of entries. |
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 | |
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 | @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
|
87 | @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
|
88 | """ |
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
|
89 | return len(self.__rawHashes) // self.__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
|
90 | |
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 | 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
|
92 | """ |
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
|
93 | Special method to iterate over the raw hashes. |
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 | |
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 | @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
|
96 | @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
|
97 | """ |
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
|
98 | n = self.__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
|
99 | return (self.__rawHashes[index:index + n] |
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
|
100 | for index in range(0, len(self.__rawHashes), n) |
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
|
101 | ) |