forked from KIJ-D-NANA/Chat-Client-Qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha1hash.cpp
59 lines (49 loc) · 1.67 KB
/
sha1hash.cpp
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
#include "sha1hash.h"
#include <openssl/sha.h>
SHA1Hash::SHA1Hash()
{
}
SHA1Hash::~SHA1Hash()
{
}
bool SHA1Hash::checkIntegrity(QString raw, QString hash){
// char raw_cstr[raw.length()];
QByteArray ba = raw.toLatin1();
QByteArray ba2 = hash.toLatin1();
return this->checkIntegrity(raw.length(), (unsigned char*)ba.data(), ba2.data());
}
bool SHA1Hash::checkIntegrity(size_t input_len, unsigned char *raw, const char *hash_string){
char hash_out[SHA_DIGEST_LENGTH + 1];
char hash_value[SHA_DIGEST_LENGTH * 2 + 1];
SHA1(raw, input_len, (unsigned char*)hash_out);
for(int i = 0; i < SHA_DIGEST_LENGTH; i++){
sprintf(&hash_value[i * 2], "%02x", (unsigned int)hash_out[i]);
}
bool Result = true;
for(int i = 0; i < SHA_DIGEST_LENGTH * 2; i++){
if(hash_value[i] != hash_string[i]){
Result = false;
break;
}
}
return Result;
}
bool SHA1Hash::checkIntegrity(std::string raw, std::string hash){
return this->checkIntegrity(raw.length(), (unsigned char*)raw.c_str(), hash.c_str());
}
QString SHA1Hash::toSHA1(QString data){
QByteArray ba = data.toLatin1();
return this->toSHA1(ba.data(), data.length());
}
QString SHA1Hash::toSHA1(std::string data){
return this->toSHA1(data.c_str(), data.size());
}
QString SHA1Hash::toSHA1(const char *data, size_t data_len){
char hash_out[SHA_DIGEST_LENGTH + 1];
char hash_value[SHA_DIGEST_LENGTH * 2 + 1];
SHA1((unsigned char*)data, data_len, (unsigned char*)hash_out);
for(int i = 0; i < SHA_DIGEST_LENGTH; i++){
sprintf(&hash_value[i * 2], "%02x", (unsigned int)hash_out[i]);
}
return QString::fromUtf8(hash_value);
}