Add support for SHA2-512 hash on Qt4 builds
[quassel.git] / src / core / storage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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 <QCryptographicHash>
24
25 #if QT_VERSION < 0x050000
26 #    include "../../3rdparty/sha512/sha512.h"
27 #endif
28
29 Storage::Storage(QObject *parent)
30     : QObject(parent)
31 {
32 }
33
34 QString Storage::hashPassword(const QString &password)
35 {
36     return hashPasswordSha2_512(password);
37 }
38
39 bool Storage::checkHashedPassword(const UserId user, const QString &password, const QString &hashedPassword, const Storage::HashVersion version)
40 {
41     bool passwordCorrect = false;
42     
43     switch (version) {
44     case Storage::HashVersion::Sha1:
45         passwordCorrect = checkHashedPasswordSha1(password, hashedPassword);
46         break;
47
48     case Storage::HashVersion::Sha2_512:
49         passwordCorrect = checkHashedPasswordSha2_512(password, hashedPassword);
50         break;
51
52     default:
53         qWarning() << "Password hash version" << QString(version) << "is not supported, please reset password";
54     }
55     
56     if (passwordCorrect && version < Storage::HashVersion::Latest) {
57         updateUser(user, password);
58     }
59     
60     return passwordCorrect;
61 }
62
63 QString Storage::hashPasswordSha1(const QString &password)
64 {
65     return QString(QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha1).toHex());
66 }
67
68 bool Storage::checkHashedPasswordSha1(const QString &password, const QString &hashedPassword)
69 {
70     return hashPasswordSha1(password) == hashedPassword;
71 }
72
73 QString Storage::hashPasswordSha2_512(const QString &password)
74 {
75     // Generate a salt of 512 bits (64 bytes) using the Mersenne Twister
76     std::random_device seed;
77     std::mt19937 generator(seed());
78     std::uniform_int_distribution<int> distribution(0, 255);
79     QByteArray saltBytes;
80     saltBytes.resize(64);
81     for (int i = 0; i < 64; i++) {
82         saltBytes[i] = (unsigned char) distribution(generator);
83     }
84     QString salt(saltBytes.toHex());
85
86     // Append the salt to the password, hash the result, and append the salt value
87     return sha2_512(password + salt) + ":" + salt;
88 }
89
90 bool Storage::checkHashedPasswordSha2_512(const QString &password, const QString &hashedPassword)
91 {
92     QRegExp colonSplitter("\\:");
93     QStringList hashedPasswordAndSalt = hashedPassword.split(colonSplitter);
94
95     if (hashedPasswordAndSalt.size() == 2){
96         return sha2_512(password + hashedPasswordAndSalt[1]) == hashedPasswordAndSalt[0];
97     }
98     else {
99         qWarning() << "Password hash and salt were not in the correct format";
100         return false;
101     }
102 }
103
104 QString Storage::sha2_512(const QString &input)
105 {
106 #if QT_VERSION >= 0x050000
107     return QString(QCryptographicHash::hash(input.toUtf8(), QCryptographicHash::Sha512).toHex());
108 #else
109     QByteArray inputBytes = input.toUtf8();
110     unsigned char output[64];
111     sha512((unsigned char*) inputBytes.constData(), inputBytes.size(), output, false);
112     return QString(QByteArray::fromRawData((char*) output, 64).toHex());
113 #endif
114 }