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