|
29 | 29 | HASH_LOOKUP_ERR_MSG = '{} is not one of the hashed items.' |
30 | 30 |
|
31 | 31 |
|
| 32 | +def sha256hex(obj): |
| 33 | + """Use Sha256 as a cryptographic hash.""" |
| 34 | + obj = obj.encode('utf-8') |
| 35 | + return sha256(obj).hexdigest() |
| 36 | + |
| 37 | + |
| 38 | +def sha1hex(obj): |
| 39 | + """Use Sha1 as a cryptographic hash.""" |
| 40 | + obj = obj.encode('utf-8') |
| 41 | + return sha1(obj).hexdigest() |
| 42 | + |
| 43 | + |
| 44 | +def murmur3_64bit(obj): |
| 45 | + """ |
| 46 | + Use murmur3_64bit for 64 bit hash by passing this method: |
| 47 | + hasher=DeepHash.murmur3_64bit |
| 48 | + """ |
| 49 | + if isinstance(obj, str): |
| 50 | + obj = obj.encode('utf-8') |
| 51 | + # This version of murmur3 returns two 64bit integers. |
| 52 | + return mmh3.hash64(obj, MURMUR_SEED)[0] |
| 53 | + |
| 54 | + |
| 55 | +def murmur3_128bit(obj): |
| 56 | + """ |
| 57 | + Use murmur3_128bit for bit hash by passing this method: |
| 58 | + hasher=DeepHash.murmur3_128bit |
| 59 | + This hasher is the default hasher. |
| 60 | + """ |
| 61 | + if isinstance(obj, str): |
| 62 | + obj = obj.encode('utf-8') |
| 63 | + return mmh3.hash128(obj, MURMUR_SEED) |
| 64 | + |
| 65 | + |
| 66 | +default_hasher = murmur3_128bit if mmh3 else sha256hex |
| 67 | + |
| 68 | + |
| 69 | +def combine_hashes_lists(items, prefix): |
| 70 | + """ |
| 71 | + Combines lists of hashes into one hash |
| 72 | + This can be optimized in future. |
| 73 | + It needs to work with both murmur3 hashes (int) and sha256 (str) |
| 74 | + """ |
| 75 | + if isinstance(prefix, bytes): |
| 76 | + prefix = prefix.decode('utf-8') |
| 77 | + hashes_bytes = b'' |
| 78 | + for item in items: |
| 79 | + # In order to make sure the order of hashes in each item does not affect the hash |
| 80 | + # we resort them. |
| 81 | + hashes_bytes += (''.join(map(str, sorted(item))) + '--').encode('utf-8') |
| 82 | + return prefix + str(default_hasher(hashes_bytes)) |
| 83 | + |
| 84 | + |
32 | 85 | class BoolObj(Enum): |
33 | 86 | TRUE = 1 |
34 | 87 | FALSE = 0 |
@@ -95,7 +148,6 @@ def __init__(self, |
95 | 148 | self.ignore_repetition = ignore_repetition |
96 | 149 | self.exclude_paths = convert_item_or_items_into_set_else_none(exclude_paths) |
97 | 150 | self.exclude_regex_paths = convert_item_or_items_into_compiled_regexes_else_none(exclude_regex_paths) |
98 | | - default_hasher = self.murmur3_128bit if mmh3 else self.sha256hex |
99 | 151 | self.hasher = default_hasher if hasher is None else hasher |
100 | 152 | self.hashes[UNPROCESSED] = [] |
101 | 153 |
|
@@ -125,37 +177,10 @@ def __init__(self, |
125 | 177 | else: |
126 | 178 | del self.hashes[UNPROCESSED] |
127 | 179 |
|
128 | | - @staticmethod |
129 | | - def sha256hex(obj): |
130 | | - """Use Sha256 as a cryptographic hash.""" |
131 | | - obj = obj.encode('utf-8') |
132 | | - return sha256(obj).hexdigest() |
133 | | - |
134 | | - @staticmethod |
135 | | - def sha1hex(obj): |
136 | | - """Use Sha1 as a cryptographic hash.""" |
137 | | - obj = obj.encode('utf-8') |
138 | | - return sha1(obj).hexdigest() |
139 | | - |
140 | | - @staticmethod |
141 | | - def murmur3_64bit(obj): |
142 | | - """ |
143 | | - Use murmur3_64bit for 64 bit hash by passing this method: |
144 | | - hasher=DeepHash.murmur3_64bit |
145 | | - """ |
146 | | - obj = obj.encode('utf-8') |
147 | | - # This version of murmur3 returns two 64bit integers. |
148 | | - return mmh3.hash64(obj, MURMUR_SEED)[0] |
149 | | - |
150 | | - @staticmethod |
151 | | - def murmur3_128bit(obj): |
152 | | - """ |
153 | | - Use murmur3_128bit for bit hash by passing this method: |
154 | | - hasher=DeepHash.murmur3_128bit |
155 | | - This hasher is the default hasher. |
156 | | - """ |
157 | | - obj = obj.encode('utf-8') |
158 | | - return mmh3.hash128(obj, MURMUR_SEED) |
| 180 | + sha256hex = sha256hex |
| 181 | + sha1hex = sha1hex |
| 182 | + murmur3_64bit = murmur3_64bit |
| 183 | + murmur3_128bit = murmur3_128bit |
159 | 184 |
|
160 | 185 | def __getitem__(self, obj, extract_index=0): |
161 | 186 | return self._getitem(self.hashes, obj, extract_index=extract_index) |
|