|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the threat list info class. |
|
8 """ |
|
9 |
|
10 |
|
11 class ThreatList: |
|
12 """ |
|
13 Class implementing the threat list info. |
|
14 """ |
|
15 def __init__(self, threatType, platformType, threatEntryType): |
|
16 """ |
|
17 Constructor |
|
18 |
|
19 @param threatType threat type |
|
20 @type str |
|
21 @param platformType platform type |
|
22 @type str |
|
23 @param threatEntryType threat entry type |
|
24 @type str |
|
25 """ |
|
26 self.threatType = threatType |
|
27 self.platformType = platformType |
|
28 self.threatEntryType = threatEntryType |
|
29 |
|
30 @classmethod |
|
31 def fromApiEntry(cls, entry): |
|
32 """ |
|
33 Class method to instantiate a threat list given a threat list entry |
|
34 dictionary. |
|
35 |
|
36 @param entry threat list entry dictionary |
|
37 @type dict |
|
38 @return instantiated object |
|
39 @rtype ThreatList |
|
40 """ |
|
41 return cls(entry['threatType'], entry['platformType'], |
|
42 entry['threatEntryType']) |
|
43 |
|
44 def asTuple(self): |
|
45 """ |
|
46 Public method to convert the object to a tuple. |
|
47 |
|
48 @return tuple containing the threat list info |
|
49 @rtype tuple of (str, str, str) |
|
50 """ |
|
51 return (self.threatType, self.platformType, self.threatEntryType) |
|
52 |
|
53 def __repr__(self): |
|
54 """ |
|
55 Special method to generate a printable representation. |
|
56 |
|
57 @return printable representation |
|
58 @rtype str |
|
59 """ |
|
60 return '/'.join(self.asTuple()) |
|
61 |
|
62 |
|
63 class HashPrefixList: |
|
64 """ |
|
65 Class implementing a container for threat list data. |
|
66 """ |
|
67 def __init__(self, prefixLength, rawHashes): |
|
68 """ |
|
69 Constructor |
|
70 |
|
71 @param prefixLength length of each hash prefix |
|
72 @type int |
|
73 @param rawHashes raw hash prefixes of given length concatenated and |
|
74 sorted in lexicographical order |
|
75 @type str |
|
76 """ |
|
77 self.__prefixLength = prefixLength |
|
78 self.__rawHashes = rawHashes |
|
79 |
|
80 def __len__(self): |
|
81 """ |
|
82 Special method to calculate the number of entries. |
|
83 |
|
84 @return length |
|
85 @rtype int |
|
86 """ |
|
87 return len(self.__rawHashes) // self.__prefixLength |
|
88 |
|
89 def __iter__(self): |
|
90 """ |
|
91 Special method to iterate over the raw hashes. |
|
92 |
|
93 @return iterator object |
|
94 @rtype iterator |
|
95 """ |
|
96 n = self.__prefixLength |
|
97 return (self.__rawHashes[index:index + n] |
|
98 for index in range(0, len(self.__rawHashes), n) |
|
99 ) |