-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
35 lines (26 loc) · 1000 Bytes
/
helpers.py
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
import hashlib
def readFile(path):
"""Read contents of file at given path as bytes."""
with open(path, 'rb') as f:
return f.read()
def writeFile(path, data):
"""Write data bytes to file at given path."""
with open(path, 'wb') as f:
f.write(data)
def generate_object_hash(data, type):
"""
Description:
Generates hash of the object including the data and it's header.
Parameters:
data (str): the object data
type (str): the object type which is one of three types (blob, commit, tree)
Return:
sha1 (SHA-1 string)): hashed object of the header and data.
"""
# the obj header contains the type of the object, and it's size
obj_header = '{} {}'.format(type, len(data)).encode()
# the object consists of the header then Null byte then it's data
obj = obj_header + b'\x00' + data
# hash the object using sha1
sha1 = hashlib.sha1(obj).hexdigest()
return sha1