def test_simple_crc32_class(self): """Verify the CRC class when not using xorOut""" crc = Crc(g32) str_rep = \ '''poly = 0x104C11DB7 reverse = True initCrc = 0xFFFFFFFF xorOut = 0x00000000 crcValue = 0xFFFFFFFF''' self.assertEqual(str(crc), str_rep) self.assertEqual(crc.digest(), '\xff\xff\xff\xff') self.assertEqual(crc.hexdigest(), 'FFFFFFFF') crc.update(self.msg) self.assertEqual(crc.crcValue, 0xF7B400A7L) self.assertEqual(crc.digest(), '\xf7\xb4\x00\xa7') self.assertEqual(crc.hexdigest(), 'F7B400A7') # Verify the .copy() method x = crc.copy() self.assertTrue(x is not crc) str_rep = \ '''poly = 0x104C11DB7 reverse = True initCrc = 0xFFFFFFFF xorOut = 0x00000000 crcValue = 0xF7B400A7''' self.assertEqual(str(crc), str_rep) self.assertEqual(str(x), str_rep)
def encrypt_and_save_to(self, warehouse): files_count = sum(len(f) for f in self.files_found.values()) self.progressBarBackup.setMaximum(files_count) self.progressBarBackup.setValue(0) now_date = datetime.datetime.now().strftime('Backup_%m-%d-%y_%H-%M') mkdir(self.current_dir + warehouse + sep + now_date + sep) for category, files in self.files_found.items(): current_dir = self.current_dir + warehouse + sep + now_date + sep + category + sep if not path.exists(current_dir): mkdir(current_dir) print(files) for file_path in files: if file_path == 'deleted': qApp.processEvents() self.progressBarBackup.setValue( self.progressBarBackup.value() + 1) continue file_name = file_path.split(sep)[-1] if not self.is_exist(warehouse, file_path, category, now_date): checksumm = Crc(0x104c11db7) checksumm.update(file_path.encode('utf-8')) checksumm.update(str( stat(file_path)[-2])) # modification time with open(file_path, 'rb') as reader, open(current_dir + file_name, 'wb') as writer: chunksize = 64 * 1024 * 16 iv = ''.join( chr(random.randint(0, 0xFF)) for _ in range(16)) cipher = AES.new( self.password + self.padding[len(self.password):], AES.MODE_CBC, iv) filesize = path.getsize(file_path) writer.write(pack('<Q', filesize)) writer.write(iv) writer.write(pack('<B', len(file_path.encode('utf-8')))) writer.write(file_path.encode('utf-8')) part = reader.read(chunksize) checksumm.update(part) while part: if len(part) % 16 != 0: part += ' ' * (16 - len(part) % 16) ciphertext = cipher.encrypt(part) writer.write(ciphertext) part = reader.read(chunksize) checksumm.update(part) rename(current_dir + file_name, current_dir + file_name + checksumm.hexdigest()) qApp.processEvents() self.progressBarBackup.setValue( self.progressBarBackup.value() + 1) self.progressBarBackup.setValue(self.progressBarBackup.maximum())
def test_full_crc32_class(self): """Verify the CRC class when using xorOut""" crc = Crc(g32, initCrc=0, xorOut=~0L) str_rep = \ '''poly = 0x104C11DB7 reverse = True initCrc = 0x00000000 xorOut = 0xFFFFFFFF crcValue = 0x00000000''' self.assertEqual(str(crc), str_rep) self.assertEqual(crc.digest(), '\x00\x00\x00\x00') self.assertEqual(crc.hexdigest(), '00000000') crc.update(self.msg) self.assertEqual(crc.crcValue, 0x84BFF58L) self.assertEqual(crc.digest(), '\x08\x4b\xff\x58') self.assertEqual(crc.hexdigest(), '084BFF58') # Verify the .copy() method x = crc.copy() self.assertTrue(x is not crc) str_rep = \ '''poly = 0x104C11DB7 reverse = True initCrc = 0x00000000 xorOut = 0xFFFFFFFF crcValue = 0x084BFF58''' self.assertEqual(str(crc), str_rep) self.assertEqual(str(x), str_rep) # Verify the .new() method y = crc.new() self.assertTrue(y is not crc) self.assertTrue(y is not x) str_rep = \ '''poly = 0x104C11DB7 reverse = True initCrc = 0x00000000 xorOut = 0xFFFFFFFF crcValue = 0x00000000''' self.assertEqual(str(y), str_rep)
def test_full_crc32_class(self): """Verify the CRC class when using xorOut""" crc = Crc(g32, initCrc=0, xorOut= ~0L) str_rep = \ '''poly = 0x104C11DB7 reverse = True initCrc = 0x00000000 xorOut = 0xFFFFFFFF crcValue = 0x00000000''' self.assertEqual(str(crc), str_rep) self.assertEqual(crc.digest(), '\x00\x00\x00\x00') self.assertEqual(crc.hexdigest(), '00000000') crc.update(self.msg) self.assertEqual(crc.crcValue, 0x84BFF58L) self.assertEqual(crc.digest(), '\x08\x4b\xff\x58') self.assertEqual(crc.hexdigest(), '084BFF58') # Verify the .copy() method x = crc.copy() self.assertTrue(x is not crc) str_rep = \ '''poly = 0x104C11DB7 reverse = True initCrc = 0x00000000 xorOut = 0xFFFFFFFF crcValue = 0x084BFF58''' self.assertEqual(str(crc), str_rep) self.assertEqual(str(x), str_rep) # Verify the .new() method y = crc.new() self.assertTrue(y is not crc) self.assertTrue(y is not x) str_rep = \ '''poly = 0x104C11DB7 reverse = True initCrc = 0x00000000 xorOut = 0xFFFFFFFF crcValue = 0x00000000''' self.assertEqual(str(y), str_rep)
def is_exist(self, storage, file_path, category, now_date): file_name = file_path.split(sep)[-1] for curdir, dirs, files in walk(self.current_dir + storage + sep): if curdir.split( sep)[-1] == category and curdir.split(sep)[-2] != now_date: for file in files: if file[:-8] == file_name: checksumm = Crc(0x104c11db7) checksumm.update(file_path.encode('utf-8')) checksumm.update(str(stat(file_path)[-2])) with open(file_path, 'rb') as reader: chunk = reader.read(1024 * 1024 * 8) while chunk: checksumm.update(chunk) chunk = reader.read(1024 * 1024 * 8) if checksumm.hexdigest() == file[-8:]: return True return False