-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashcalculator.cpp
More file actions
101 lines (81 loc) · 2.89 KB
/
hashcalculator.cpp
File metadata and controls
101 lines (81 loc) · 2.89 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
#include "hashcalculator.h"
#include <fuzzy.h>
#include <stdexcept>
HashCalculator::HashCalculator(QString filename) {
SSL_library_init();
OpenSSL_add_all_digests();
calculateHashes(filename);
}
HashCalculator::~HashCalculator() {
EVP_cleanup();
}
bool HashCalculator::calculateHashes(const QString& fileName) {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
// Determine file size
qint64 fileSize = file.size();
// Allocate buffer for file contents
QByteArray fileContents = file.readAll();
// Calculate hashes
calculateMD5((const unsigned char*) fileContents.constData(), fileContents.size());
calculateSHA512((const unsigned char*) fileContents.constData(), fileContents.size());
calculateSHA256((const unsigned char*) fileContents.constData(), fileContents.size());
calculateSSDeep((const unsigned char*) fileContents.constData(), fileContents.size());
return true;
}
QString HashCalculator::getMD5() const {
return m_md5;
}
QString HashCalculator::getSHA512() const {
return m_sha512;
}
QString HashCalculator::getSHA256() const {
return m_sha256;
}
QString HashCalculator::getSSDeep() const {
return m_ssdeep;
}
void HashCalculator::calculateMD5(const unsigned char* data, size_t length) {
unsigned char md5Digest[MD5_DIGEST_LENGTH];
MD5(data, length, md5Digest);
QString result;
result.reserve(MD5_DIGEST_LENGTH * 2);
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
result.append(QString::number(md5Digest[i], 16).rightJustified(2, '0'));
}
m_md5 = result;
}
void HashCalculator::calculateSHA512(const unsigned char* data, size_t length) {
unsigned char sha512Digest[SHA512_DIGEST_LENGTH];
SHA512(data, length, sha512Digest);
QString result;
result.reserve(SHA512_DIGEST_LENGTH * 2);
for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) {
result.append(QString::number(sha512Digest[i], 16).rightJustified(2, '0'));
}
m_sha512 = result;
}
void HashCalculator::calculateSHA256(const unsigned char* data, size_t length) {
unsigned char sha256Digest[SHA256_DIGEST_LENGTH];
SHA256(data, length, sha256Digest);
QString result;
result.reserve(SHA256_DIGEST_LENGTH * 2);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
result.append(QString::number(sha256Digest[i], 16).rightJustified(2, '0'));
}
m_sha256 = result;
}
void HashCalculator::calculateSSDeep(const unsigned char* data, size_t length) {
unsigned char sha1Digest[SHA_DIGEST_LENGTH];
SHA1(data, length, sha1Digest);
char ssdeepDigest[4096];
memset(ssdeepDigest, 0, sizeof(ssdeepDigest));
int ssdeepResult = fuzzy_hash_buf((const unsigned char*) data, length, ssdeepDigest);
if (ssdeepResult == 0) {
m_ssdeep = QString::fromUtf8(ssdeepDigest);
} else {
// qWarning() << "Failed to calculate SSDeep hash";
}
}