-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordHasher.cpp
60 lines (51 loc) · 1.66 KB
/
PasswordHasher.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
/*
* SEGS - Super Entity Game Server
* http://www.segs.io/
* Copyright (c) 2006 - 2019 SEGS Team (see AUTHORS.md)
* This software is licensed under the terms of the 3-clause BSD License. See LICENSE.md for details.
*/
/*!
* @addtogroup Components
* @{
*/
#include "PasswordHasher.h"
#include <QTime>
PasswordHasher::PasswordHasher() : m_hasher(QCryptographicHash::Sha256)
{
}
QString PasswordHasher::getRandomString(int length) const
{
const QString possibleCharacters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
const int randomStringLength = length;
qsrand(static_cast<quint64>(QTime::currentTime().msecsSinceStartOfDay()));
QString randomString;
for(int i = 0; i < randomStringLength; ++i)
{
int index = qrand() % possibleCharacters.length();
QChar nextChar = possibleCharacters.at(index);
randomString.append(nextChar);
}
return randomString;
}
/*!
* \brief Generates a random salt of length 16.
* \return A QByteArray of length 16 containing the generated salt.
*/
QByteArray PasswordHasher::generateSalt()
{
QString salt = getRandomString(16);
return salt.toUtf8();
}
/*!
* \brief Hashes a password with the given salt using the Sha256 algorithm.
* \param pass The password to be hashed.
* \param salt The salt to be appended to the password.
* \return A QByteArray containing the salted and hashed password.
*/
QByteArray PasswordHasher::hashPassword(const QByteArray &pass, const QByteArray &salt)
{
QByteArray pass_array(pass+salt);
QByteArray hashed_pass_array = QCryptographicHash::hash(pass_array, QCryptographicHash::Sha256);
return hashed_pass_array;
}
//! @}