1
|
|
|
try: |
2
|
|
|
from hashlib import md5, sha256, sha1, blake2b, blake2s, sha512, shake_128 |
3
|
|
|
from xxhash import xxh32, xxh64 |
4
|
|
|
|
5
|
|
View Code Duplication |
class _HashAlgos: |
|
|
|
|
6
|
|
|
|
7
|
|
|
def __init__(self, string): |
8
|
|
|
self.string = string |
9
|
|
|
|
10
|
|
|
self._md5 = None |
11
|
|
|
self._sha1 = None |
12
|
|
|
self._sha256 = None |
13
|
|
|
self._sha512 = None |
14
|
|
|
self._blake2b = None |
15
|
|
|
self._blake2s = None |
16
|
|
|
self._shake_128 = None |
17
|
|
|
self._xxh32 = None |
18
|
|
|
self._xxh64 = None |
19
|
|
|
|
20
|
|
|
def _get_md5(self): |
21
|
|
|
"""Hash a string using the md5 algo and return in hexdigested form.""" |
22
|
|
|
return md5(self.string).hexdigest() |
23
|
|
|
|
24
|
|
|
def _get_sha1(self): |
25
|
|
|
"""Hash a string using the sha256 algo and return in hexdigested form.""" |
26
|
|
|
return sha1(self.string).hexdigest() |
27
|
|
|
|
28
|
|
|
def _get_sha256(self): |
29
|
|
|
"""Hash a string using the sha256 algo and return in hexdigested form.""" |
30
|
|
|
return sha256(self.string).hexdigest() |
31
|
|
|
|
32
|
|
|
def _get_sha512(self): |
33
|
|
|
"""Hash a string using the sha512algo and return in hexdigested form.""" |
34
|
|
|
return sha512(self.string).hexdigest() |
35
|
|
|
|
36
|
|
|
def _get_blake2b(self): |
37
|
|
|
"""Hash a string using the blake2b algo and return in hexdigested form.""" |
38
|
|
|
return blake2b(self.string).hexdigest() |
39
|
|
|
|
40
|
|
|
def _get_blake2s(self): |
41
|
|
|
"""Hash a string using the blake2b algo and return in hexdigested form.""" |
42
|
|
|
return blake2s(self.string).hexdigest() |
43
|
|
|
|
44
|
|
|
def _get_shake_128(self): |
45
|
|
|
"""Hash a string using the shake_128 algo and return in hexdigested form.""" |
46
|
|
|
return shake_128(self.string).hexdigest(128) |
47
|
|
|
|
48
|
|
|
def _get_xxh32(self): |
49
|
|
|
"""Hash a string using the xxh32 algo and return in hexdigested form.""" |
50
|
|
|
return xxh32(self.string).hexdigest() |
51
|
|
|
|
52
|
|
|
def _get_xxh64(self): |
53
|
|
|
"""Hash a string using the xxh64 algo and return in hexdigested form.""" |
54
|
|
|
return xxh64(self.string).hexdigest() |
55
|
|
|
|
56
|
|
View Code Duplication |
class Hash(_HashAlgos): |
|
|
|
|
57
|
|
|
|
58
|
|
|
def __init__(self, string): |
59
|
|
|
super(Hash, self).__init__(string) |
60
|
|
|
|
61
|
|
|
def md5(self): |
62
|
|
|
"""Return a hexdigested md5 hash.""" |
63
|
|
|
if not self._md5: |
64
|
|
|
self._md5 = self._get_md5() |
65
|
|
|
return self._md5 |
66
|
|
|
|
67
|
|
|
def sha1(self): |
68
|
|
|
"""Return a hexdigested sha256 hash.""" |
69
|
|
|
if not self._sha1: |
70
|
|
|
self._sha1 = self._get_sha1() |
71
|
|
|
return self._sha1 |
72
|
|
|
|
73
|
|
|
def sha256(self): |
74
|
|
|
"""Return a hexdigested sha256 hash.""" |
75
|
|
|
if not self._sha256: |
76
|
|
|
self._sha256 = self._get_sha256() |
77
|
|
|
return self._sha256 |
78
|
|
|
|
79
|
|
|
def sha512(self): |
80
|
|
|
"""Return a hexdigested sha256 hash.""" |
81
|
|
|
if not self._sha512: |
82
|
|
|
self._sha512 = self._get_sha512() |
83
|
|
|
return self._sha512 |
84
|
|
|
|
85
|
|
|
def blake2b(self): |
86
|
|
|
"""Return a hexdigested blake2b hash.""" |
87
|
|
|
if not self._blake2b: |
88
|
|
|
self._blake2b = self._get_blake2b() |
89
|
|
|
return self._blake2b |
90
|
|
|
|
91
|
|
|
def blake2s(self): |
92
|
|
|
"""Return a hexdigested blake2s hash.""" |
93
|
|
|
if not self._blake2s: |
94
|
|
|
self._blake2s = self._get_blake2s() |
95
|
|
|
return self._blake2s |
96
|
|
|
|
97
|
|
|
def shake_128(self): |
98
|
|
|
"""Return a hexdigested shake_128 hash.""" |
99
|
|
|
if not self._shake_128: |
100
|
|
|
self._shake_128 = self._get_shake_128() |
101
|
|
|
return self._shake_128 |
102
|
|
|
|
103
|
|
|
def xxh32(self): |
104
|
|
|
"""Return a hexdigested xxh32 hash.""" |
105
|
|
|
if not self._xxh32: |
106
|
|
|
self._xxh32 = self._get_xxh32() |
107
|
|
|
return self._xxh32 |
108
|
|
|
|
109
|
|
|
def xxh64(self): |
110
|
|
|
"""Return a hexdigested xxh64 hash.""" |
111
|
|
|
if not self._xxh64: |
112
|
|
|
self._xxh64 = self._get_xxh64() |
113
|
|
|
return self._xxh64 |
114
|
|
|
|
115
|
|
|
except ImportError: |
116
|
|
|
|
117
|
|
|
class Hash: |
118
|
|
|
|
119
|
|
|
def __init__(self, *args, **kwargs): |
120
|
|
|
pass |
121
|
|
|
|