-
Notifications
You must be signed in to change notification settings - Fork 86
/
generate_hashes.py
103 lines (70 loc) · 2.32 KB
/
generate_hashes.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import hashlib
import os
import struct
import sys
BUFF_SIZE = 65536
def generate_hashes(files, root, fm_folder):
header = """#pragma once
#include <Windows.h>
static WCHAR* files[] = {}
static BYTE hashes[][32] = {}
"""
file_string = "{\n"
hash_string = "{\n"
print("==== Hashes ====")
for file in files:
sha1 = hashlib.sha1()
path = os.path.join(root, file)
file_string += "\tL\"" + file + "\"" + ',\n'
f = open (path, 'rb')
while True:
data = f.read(BUFF_SIZE)
if not data:
break
sha1.update(data)
f.close()
print("File: {}, SHA1: {}".format(file, sha1.hexdigest()))
digest = sha1.digest()
hash_string += "\t{"
for i in range(0, len(digest)):
#print(digest[i])
#value = struct.unpack('B', bytes(digest[i]))[0]
value = digest[i]
hash_string += "0x{:02x}".format(value) + ','
hash_string = hash_string[:-1]
hash_string += "},\n"
hash_string = hash_string[:-2]
hash_string += "\n};"
file_string= file_string[:-1]
file_string += "\n};"
header_file = open(os.path.join(fm_folder, "Hashes.h"), "w+")
header_file.write(header.format(file_string, hash_string))
header_file.close()
def normalise_file_endings(file):
backup = file + "_"
os.rename(file, file + "_")
fileRead = open(file + "_", "rb")
if fileRead:
text = fileRead.read()
fileRead.close()
text = text.replace(b'\r\n',b'\n')
fileWrite = open(file, "wb+")
if fileWrite:
fileWrite.write(text)
os.remove(file + "_")
print("{} line endings normalised.".format(file))
else:
print("Error Occured in Write.")
else:
print("Error Occured in Read.")
def normalise_files_endings(files, root):
for file in files:
normalise_file_endings(os.path.join(root, file))
def main(files, root, fm_folder):
normalise_files_endings(files, root)
generate_hashes(files, root, fm_folder)
if __name__ == "__main__":
files = ["entry.lua","A-4E-C.lua", "Cockpit/Scripts/EFM_Data_Bus.lua", "Entry/Suspension.lua"]
root = "A-4E-C"
fm_folder = "A-4E-C/ExternalFM/FM"
main(files, root, fm_folder)