Shred Function Qt C++ on windows
So, I searched for such a function and, I did find some things on O-Stack, but I rewrote what I found because it didn't actually work, I tested this function and it works pretty well, I think it has room for improvement as usual but I thought to share it anyway.
So here it is:
bool QtClAct::shred(const QString & fileName, uint RepeatWrite, bool DeleteFileLink) {
if(RepeatWrite < 1) RepeatWrite = 1;
QSaveFile file(fileName);
QFileInfo fi(file.fileName());
for(int i=0; i < RepeatWrite; i++){ qint64 fileSize = fi.size(); if (! file.open(QIODevice::WriteOnly )) return false; QByteArray block(65536, '\0'); while (fileSize > 0) {
if (fileSize < block.size()) block.truncate(fileSize);
qint64 written = file.write(block);
if (written != block.size()) return false;
fileSize -= written;
}
}
//Q_ASSERT(fileSize == 0);
file.commit();
if (DeleteFileLink){
QFile file(fileName);
file.remove();
}
return true;
}
I defined this method in my header with only the first argument required. I did a bit of testing with Recuva and seems to work but again it may need more testing and maybe even a rewrite.
I think instead of nulls you can add maybe some random bytes, anyway as you can see the function can leave the file just scrambled and don't remove the actual filename reference. The main idea is to make the file unrecoverable.
Also, this function could work on Linux too since I used only Qt classes but honestly, I didn't test to see that.