-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.cpp
60 lines (46 loc) · 1.55 KB
/
utils.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
60
/**
* The utils class have the fuctions that are required to generate the password digest.
*
* The functions such as getEncodedNonce, getPasswordDigest.
*/
#include <QTime>
#include <QCryptographicHash>
#include "utils.h"
Utils::Utils(){}
Utils::~Utils(){}
// getting the random nonce
int Utils::getNonce() {
QTime now = QTime::currentTime();
uint no = (uint)now.msec();
qsrand(no);
int ret = qrand();
return ret;
}
// getting the base64 encoded nonce
QString Utils::getNonceEncoded() {
//generated nonce no.
QString gen_nonce = QString::number(this->getNonce());
QByteArray nonce_str(gen_nonce.toStdString().c_str());
//get base64 of nonce
QString ret(nonce_str.toBase64());
return ret;
}
// getting the password digest
QString Utils::getPasswordDigest(QString nonce, QString date_time, QString pass_word) {
// base64 encoded nonce
QString base64_enc_nonce(nonce);
//nonce bytes
QByteArray nonce_bytes = QByteArray::fromBase64 (base64_enc_nonce.toUtf8());
QByteArray byteArray_for_hash(nonce_bytes);
//date time bytes
QString utc_datetime (date_time);
QByteArray datetime (utc_datetime.toUtf8());
byteArray_for_hash.append (datetime);
//password bytes
QByteArray password(pass_word.toUtf8());
byteArray_for_hash.append(password);
//getting SHA1 and Base64
QByteArray password_sha1 = QCryptographicHash::hash(byteArray_for_hash, QCryptographicHash::Sha1);
QByteArray password_Digest(password_sha1.toBase64());
return QString::fromLocal8Bit(password_Digest);
}