/
password.cpp
87 lines (75 loc) · 1.86 KB
/
password.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
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
#include "multiplayer/password.h"
#include "coreengine/filesupport.h"
#include "coreengine/interpreter.h"
#include <QCryptographicHash>
Password::Password()
{
Interpreter::setCppOwnerShip(this);
setPassword("");
}
Password::Password(QString password)
{
Interpreter::setCppOwnerShip(this);
setPassword(password);
}
Password::Password(const Password & password)
: m_passwordHash(password.m_passwordHash),
m_password(password.m_password),
m_isSet(password.m_isSet)
{
Interpreter::setCppOwnerShip(this);
}
void Password::serializeObject(QDataStream& pStream) const
{
pStream << getVersion();
Filesupport::writeByteArray(pStream, m_passwordHash);
}
void Password::deserializeObject(QDataStream& pStream)
{
qint32 version = 0;
pStream >> version;
m_passwordHash = Filesupport::readByteArray(pStream);
}
void Password::setPassword(const QString password)
{
m_password = password;
if (password.isEmpty())
{
m_passwordHash = QByteArray();
m_isSet = false;
}
else
{
QCryptographicHash myHash(QCryptographicHash::Sha512);
myHash.addData(password.toUtf8());
m_passwordHash = myHash.result();
m_isSet = true;
}
}
QString Password::getPasswordText() const
{
return m_password;
}
void Password::setPassword(const Password & password)
{
m_passwordHash = password.m_passwordHash;
}
bool Password::isValidPassword(const QString password) const
{
QByteArray data;
if (!password.isEmpty())
{
QCryptographicHash myHash(QCryptographicHash::Sha512);
myHash.addData(password.toUtf8());
data = myHash.result();
}
return (m_passwordHash == data);
}
bool Password::areEqualPassword(const Password & password) const
{
return (m_passwordHash == password.m_passwordHash);
}
bool Password::getIsSet() const
{
return m_isSet;
}