keepassxc

Форк
0
/
PasswordKey.cpp 
90 строк · 2.2 Кб
1
/*
2
 *  Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
3
 *
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.
8
 *
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.
13
 *
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/>.
16
 */
17

18
#include "PasswordKey.h"
19

20
#include "crypto/CryptoHash.h"
21

22
#include <QDataStream>
23
#include <QSharedPointer>
24

25
QUuid PasswordKey::UUID("77e90411-303a-43f2-b773-853b05635ead");
26

27
constexpr int PasswordKey::SHA256_SIZE;
28

29
PasswordKey::PasswordKey()
30
    : Key(UUID)
31
    , m_key(SHA256_SIZE)
32
{
33
}
34

35
PasswordKey::PasswordKey(const QString& password)
36
    : Key(UUID)
37
    , m_key(SHA256_SIZE)
38
{
39
    setPassword(password);
40
}
41

42
QByteArray PasswordKey::rawKey() const
43
{
44
    if (!m_isInitialized) {
45
        return {};
46
    }
47
    return {m_key.data(), int(m_key.size())};
48
}
49

50
void PasswordKey::setRawKey(const QByteArray& data)
51
{
52
    if (data.isEmpty()) {
53
        m_key.clear();
54
        m_isInitialized = false;
55
    } else {
56
        Q_ASSERT(data.size() == SHA256_SIZE);
57
        m_key.assign(data.begin(), data.end());
58
        m_isInitialized = true;
59
    }
60
}
61

62
void PasswordKey::setPassword(const QString& password)
63
{
64
    setRawKey(CryptoHash::hash(password.toUtf8(), CryptoHash::Sha256));
65
}
66

67
QSharedPointer<PasswordKey> PasswordKey::fromRawKey(const QByteArray& rawKey)
68
{
69
    auto result = QSharedPointer<PasswordKey>::create();
70
    result->setRawKey(rawKey);
71
    return result;
72
}
73

74
QByteArray PasswordKey::serialize() const
75
{
76
    QByteArray data;
77
    QDataStream stream(&data, QIODevice::WriteOnly);
78
    stream << uuid().toRfc4122() << rawKey();
79
    return data;
80
}
81

82
void PasswordKey::deserialize(const QByteArray& data)
83
{
84
    QByteArray uuidData, key;
85
    QDataStream stream(data);
86
    stream >> uuidData >> key;
87
    if (uuid().toRfc4122() == uuidData) {
88
        setRawKey(key);
89
    }
90
}
91

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.