2
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "PasswordKey.h"
20
#include "crypto/CryptoHash.h"
23
#include <QSharedPointer>
25
QUuid PasswordKey::UUID("77e90411-303a-43f2-b773-853b05635ead");
27
constexpr int PasswordKey::SHA256_SIZE;
29
PasswordKey::PasswordKey()
35
PasswordKey::PasswordKey(const QString& password)
39
setPassword(password);
42
QByteArray PasswordKey::rawKey() const
44
if (!m_isInitialized) {
47
return {m_key.data(), int(m_key.size())};
50
void PasswordKey::setRawKey(const QByteArray& data)
54
m_isInitialized = false;
56
Q_ASSERT(data.size() == SHA256_SIZE);
57
m_key.assign(data.begin(), data.end());
58
m_isInitialized = true;
62
void PasswordKey::setPassword(const QString& password)
64
setRawKey(CryptoHash::hash(password.toUtf8(), CryptoHash::Sha256));
67
QSharedPointer<PasswordKey> PasswordKey::fromRawKey(const QByteArray& rawKey)
69
auto result = QSharedPointer<PasswordKey>::create();
70
result->setRawKey(rawKey);
74
QByteArray PasswordKey::serialize() const
77
QDataStream stream(&data, QIODevice::WriteOnly);
78
stream << uuid().toRfc4122() << rawKey();
82
void PasswordKey::deserialize(const QByteArray& data)
84
QByteArray uuidData, key;
85
QDataStream stream(data);
86
stream >> uuidData >> key;
87
if (uuid().toRfc4122() == uuidData) {