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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| import time,re,os
from watchdog.events import * from watchdog.observers import Observer
def rotation_left(x, num): num %= 32 left = (x << num) % (2 ** 32) right = (x >> (32 - num)) % (2 ** 32) result = left ^ right return result
def Int2Bin(x, k): x = str(bin(x)[2:]) result = "0" * (k - len(x)) + x return result
class SM3:
def __init__(self): self.IV = [0x7380166F, 0x4914B2B9, 0x172442D7, 0xDA8A0600, 0xA96F30BC, 0x163138AA, 0xE38DEE4D, 0xB0FB0E4E] self.T = [0x79cc4519, 0x7a879d8a] self.maxu32 = 2 ** 32 self.w1 = [0] * 68 self.w2 = [0] * 64
def ff(self, x, y, z, j): result = 0 if j < 16: result = x ^ y ^ z elif j >= 16: result = (x & y) | (x & z) | (y & z) return result
def gg(self, x, y, z, j): result = 0 if j < 16: result = x ^ y ^ z elif j >= 16: result = (x & y) | (~x & z) return result
def p(self, x, mode): result = 0 if mode == 0: result = x ^ rotation_left(x, 9) ^ rotation_left(x, 17) elif mode == 1: result = x ^ rotation_left(x, 15) ^ rotation_left(x, 23) return result
def sm3_fill(self, msg): length = len(msg) l = length * 8
num = length // 64 remain_byte = length % 64 msg_remain_bin = "" msg_new_bytes = bytearray((num + 1) * 64)
for i in range(length): msg_new_bytes[i] = msg[i]
remain_bit = remain_byte * 8 for i in range(remain_byte): msg_remain_bin += "{:08b}".format(msg[num * 64 + i])
k = (448 - l - 1) % 512 while k < 0: k += 512
msg_remain_bin += "1" + "0" * k + Int2Bin(l, 64)
for i in range(0, 64 - remain_byte): str = msg_remain_bin[i * 8 + remain_bit: (i + 1) * 8 + remain_bit] temp = length + i msg_new_bytes[temp] = int(str, 2) return msg_new_bytes
def sm3_msg_extend(self, msg): for i in range(0, 16): self.w1[i] = int.from_bytes(msg[i * 4:(i + 1) * 4], byteorder="big")
for i in range(16, 68): self.w1[i] = self.p(self.w1[i - 16] ^ self.w1[i - 9] ^ rotation_left(self.w1[i - 3], 15), 1) ^ rotation_left(self.w1[i - 13], 7) ^ self.w1[i - 6]
for i in range(64): self.w2[i] = self.w1[i] ^ self.w1[i + 4]
def sm3_compress(self, msg):
self.sm3_msg_extend(msg) ss1 = 0
A = self.IV[0] B = self.IV[1] C = self.IV[2] D = self.IV[3] E = self.IV[4] F = self.IV[5] G = self.IV[6] H = self.IV[7]
for j in range(64): if j < 16: ss1 = rotation_left((rotation_left(A, 12) + E + rotation_left(self.T[0], j)) % self.maxu32, 7) elif j >= 16: ss1 = rotation_left((rotation_left(A, 12) + E + rotation_left(self.T[1], j)) % self.maxu32, 7) ss2 = ss1 ^ rotation_left(A, 12) tt1 = (self.ff(A, B, C, j) + D + ss2 + self.w2[j]) % self.maxu32 tt2 = (self.gg(E, F, G, j) + H + ss1 + self.w1[j]) % self.maxu32 D = C C = rotation_left(B, 9) B = A A = tt1 H = G G = rotation_left(F, 19) F = E E = self.p(tt2, 0)
self.IV[0] ^= A self.IV[1] ^= B self.IV[2] ^= C self.IV[3] ^= D self.IV[4] ^= E self.IV[5] ^= F self.IV[6] ^= G self.IV[7] ^= H
def sm3_update(self, msg): msg_new = self.sm3_fill(msg) n = len(msg_new) // 64
for i in range(0, n): self.sm3_compress(msg_new[i * 64:(i + 1) * 64])
def sm3_final(self): digest_str = "" for i in range(len(self.IV)): digest_str += hex(self.IV[i])[2:]
return digest_str.upper()
def hashFile(self, filename): with open(filename, 'rb') as fp: contents = fp.read() self.sm3_update(bytearray(contents)) return self.sm3_final()
def getFileName(path): ''' 获取指定目录下的所有指定后缀的文件名 ''' f_list = os.listdir(path) list = [] for i in f_list: if os.path.splitext(i)[1] == '.tsidx': list.append(i) return list
class FileEventHandler(FileSystemEventHandler): def __init__(self): FileSystemEventHandler.__init__(self)
def on_created(self, event): if event.is_directory: print(event.src_path) time.sleep(15) if re.compile("db_\d+_\d+_\d+").findall(event.src_path.split("/")[-1]): list = getFileName(event.src_path) for i in range(len(list)): file_digest = SM3().hashFile(event.src_path + "/" + list[i]) file = open(event.src_path + "/sm3(" + str(i) + ").txt", "a") file.write(file_digest+ " " + list[i] + "\n") file.close()
if __name__ == "__main__": observer = Observer() event_handler = FileEventHandler() observer.schedule(event_handler, "/tmp", True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
|