test: Add build system support and a main function for unit tests
[quassel.git] / src / core / storage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "storage.h"
22
23 #include <random>
24
25 #include <QCryptographicHash>
26
27 Storage::Storage(QObject *parent)
28     : QObject(parent)
29 {
30 }
31
32 QString Storage::hashPassword(const QString &password)
33 {
34     return hashPasswordSha2_512(password);
35 }
36
37 bool Storage::checkHashedPassword(const UserId user, const QString &password, const QString &hashedPassword, const Storage::HashVersion version)
38 {
39     bool passwordCorrect = false;
40
41     switch (version) {
42     case Storage::HashVersion::Sha1:
43         passwordCorrect = checkHashedPasswordSha1(password, hashedPassword);
44         break;
45
46     case Storage::HashVersion::Sha2_512:
47         passwordCorrect = checkHashedPasswordSha2_512(password, hashedPassword);
48         break;
49
50     default:
51         qWarning() << "Password hash version" << QString(version) << "is not supported, please reset password";
52     }
53
54     if (passwordCorrect && version < Storage::HashVersion::Latest) {
55         updateUser(user, password);
56     }
57
58     return passwordCorrect;
59 }
60
61 QString Storage::hashPasswordSha1(const QString &password)
62 {
63     return QString(QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha1).toHex());
64 }
65
66 bool Storage::checkHashedPasswordSha1(const QString &password, const QString &hashedPassword)
67 {
68     return hashPasswordSha1(password) == hashedPassword;
69 }
70
71 QString Storage::hashPasswordSha2_512(const QString &password)
72 {
73     // Generate a salt of 512 bits (64 bytes) using the Mersenne Twister
74     std::random_device seed;
75     std::mt19937 generator(seed());
76     std::uniform_int_distribution<int> distribution(0, 255);
77     QByteArray saltBytes;
78     saltBytes.resize(64);
79     for (int i = 0; i < 64; i++) {
80         saltBytes[i] = (unsigned char) distribution(generator);
81     }
82     QString salt(saltBytes.toHex());
83
84     // Append the salt to the password, hash the result, and append the salt value
85     return sha2_512(password + salt) + ":" + salt;
86 }
87
88 bool Storage::checkHashedPasswordSha2_512(const QString &password, const QString &hashedPassword)
89 {
90     QRegExp colonSplitter("\\:");
91     QStringList hashedPasswordAndSalt = hashedPassword.split(colonSplitter);
92
93     if (hashedPasswordAndSalt.size() == 2){
94         return sha2_512(password + hashedPasswordAndSalt[1]) == hashedPasswordAndSalt[0];
95     }
96     else {
97         qWarning() << "Password hash and salt were not in the correct format";
98         return false;
99     }
100 }
101
102 QString Storage::sha2_512(const QString &input)
103 {
104     return QString(QCryptographicHash::hash(input.toUtf8(), QCryptographicHash::Sha512).toHex());
105 }