-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhmac_helper.py
More file actions
51 lines (39 loc) · 1.46 KB
/
hmac_helper.py
File metadata and controls
51 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import hashlib, binascii, hmac
import sys, os, io
from binascii import hexlify
import crypto
import operator
import struct
CHUNK_SIZE = 16
class HashHelper(object):
def __init__(self, stream, hkey=None):
self.stream = stream
self.crypto_helper = crypto.AESCipher()
if hkey is not None:
self.hkey = hkey
else:
raise AttributeError("Must Supply an HMAC Key <self.hkey> to object initialization")
self.sha256 = None
#self.sha256 = hmac.HMAC(key=self.hkey, digestmod="SHA256")
return
def calc_digest(self):
self.sha256 = hmac.HMAC(key=self.hkey, digestmod="sha256")
try:
word = self.stream.read(CHUNK_SIZE)
while word:
self.sha256.update(word)
word = self.stream.read(CHUNK_SIZE)
digest = self.sha256.hexdigest()
finally:
#print("Finished, closing file.")
self.stream.close()
#print("Digest Finished")
print("New Digest Value", binascii.hexlify(self.sha256.digest()))
print("Digest Size", self.sha256.digest_size)
print("Block Size", self.sha256.block_size)
return(self.sha256.digest())
if __name__ == "__main__":
print("Remember to hard code an hkey to test with this file")
stream = io.BytesIO(open("temp_decrypted.bin", "rb").read())
h = HashHelper(stream, hkey=b'')
h.calc_digest()