-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfingerprintandtraces.cpp
More file actions
139 lines (125 loc) · 5.23 KB
/
fingerprintandtraces.cpp
File metadata and controls
139 lines (125 loc) · 5.23 KB
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
#include "fingerprintandtraces.h"
#include <QDebug>
FingerPrintAndTraces::FingerPrintAndTraces(QObject *parent) :
QThread(parent)
{
}
QByteArray FingerPrintAndTraces::makeHash(QString hashtype, QString targetToHashing)
{
QFile file(targetToHashing);
if (!file.exists()){
log("[warning] Arquivo indicado para aplicar digest não existe. Operação cancelada. ");
}
QMap <QString, QCryptographicHash::Algorithm> hashes;
hashes.insert("Md4",QCryptographicHash::Md4);
hashes.insert("Md5",QCryptographicHash::Md5);
hashes.insert("Sha1",QCryptographicHash::Sha1);
hashes.insert("Sha224",QCryptographicHash::Sha224);
hashes.insert("Sha256",QCryptographicHash::Sha256);
hashes.insert("Sha384",QCryptographicHash::Sha384);
hashes.insert("Sha512",QCryptographicHash::Sha512);
hashes.insert("Sha3_224",QCryptographicHash::Sha3_224);
hashes.insert("Sha3_256",QCryptographicHash::Sha3_256);
hashes.insert("Sha3_384",QCryptographicHash::Sha3_384);
hashes.insert("Sha3_512",QCryptographicHash::Sha3_512);
if (!hashes.keys().contains(hashtype)){
log("[warning] digest escolhido é invalido. Usando sha1. ");
hashtype = "Sha1";
}
QCryptographicHash crypto(hashes[hashtype]);
file.open(QFile::ReadOnly);
while(!file.atEnd()){
crypto.addData(file.read(8192));
}
QByteArray hash = crypto.result();
hash = hash.toHex();
return hash;
}
void FingerPrintAndTraces::isHashPossible()
{
QStringList msg;
//não é pra gerar hash
/*
* "sha1sum /media/images/UNKNOWN_DEVICE.dd"
*/
QString saveFileOnThisPlace = this->hashToFile;
this->hashToFile.clear();
//2 - SÓ EXECUTAR SE O ARQUIVO FOR .DD
//3 - PBARUNDEF SERIA BOM NESSE MOMENTO
//1 - LOOP COM O NOME DO ARQUIVO SEM A EXTENSAO
QString filename = saveFileOnThisPlace.split("/").last();
QString dirname = saveFileOnThisPlace.remove(filename);
QDir directory(dirname);
QStringList entries = directory.entryList();
QString filenameWithoutExtension = filename.remove(".dd");
filenameWithoutExtension.remove(".000");
filenameWithoutExtension.remove(".aa");
filenameWithoutExtension.remove(QRegExp("\\.sha\\d{1,}",Qt::CaseInsensitive));
filenameWithoutExtension.remove(QRegExp("\\.md\\d"));
QString hashes = "";
int written = -1;
for (int i=2;i<entries.length();i++){
if (entries.at(i).contains(filenameWithoutExtension) && (!entries.at(i).contains("Sha")||!entries.at(i).contains("Md5"))){
QByteArray result = makeHash(this->algorithym,dirname+entries.at(i));
if (result.length() > 5){
emit log("[step] hash do arquivo "+entries.at(i).toUtf8()+":"+result);
QString filenameAndPath = dirname + filename;
if (!filenameAndPath.isEmpty()){
QFile file(filenameAndPath);
if (file.exists()){
if (this->writeHashInFile && !file.open(QIODevice::Text|QIODevice::Append)){
emit log("Não foi possível gravar o hash em arquivo (openning). ");
return;
}
}
else{
if (this->writeHashInFile && !file.open(QIODevice::Text|QIODevice::WriteOnly)){
emit log("Não foi possível gravar o hash em arquivo (openning). ");
return;
}
}
QByteArray infoHash = result + " " + entries.at(i).toUtf8() + "\n";
if (this->writeHashInFile){
written = file.write(infoHash);
}
hashes += infoHash;
if (written < 0 && this->writeHashInFile){
msg.clear();
msg << tr("Não foi possível gravar o hash em arquivo (written).") << "" << "";
//emit signalMsgBox(msg);
emit log("Não foi possível gravar o hash em arquivo (written). ");
}
if (this->writeHashInFile){
file.close(); //gravar em arquivo finalizado, com ou sem sucesso
}
}
}
else{
msg.clear();
msg << tr("Não foi possível ler o resultado do cálculo hash. ") << "" << "";
//emit signalMsgBox(msg);
emit log("Não foi possível ler o resultado do cálculo hash. ");
}
emit log("Hash finalizado: ");
}
}
msg.clear();
msg << hashes << "Hashes";
emit signalMsgBox(msg);
}
void FingerPrintAndTraces::run()
{
QStringList msg;
emit log("Cálculo do hash iniciado:");
msg << tr("Cálculo do hash iniciado. Por favor, aguarde, o hash será exibido ao final desta operação.") << "" << "";
emit signalMsgBox(msg);
this->isHashPossible();
}
void FingerPrintAndTraces::setHashType(QString hashSelected, QString fileTarget, bool writeInAfile)
{
this->algorithym = hashSelected;
this->hashToFile.clear();
// /path/file.sha1 por exemplo
this->hashToFile = fileTarget;
this->writeHashInFile = writeInAfile;
}