1 | // Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> |
2 | // Copyright (C) 2016 The Qt Company Ltd. |
3 | // Copyright (C) 2013 Richard J. Moore <rich@kde.org>. |
4 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
5 | |
6 | #ifndef QCRYPTOGRAPHICHASH_H |
7 | #define QCRYPTOGRAPHICHASH_H |
8 | |
9 | #include <QtCore/qbytearray.h> |
10 | #include <QtCore/qobjectdefs.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | |
15 | class QCryptographicHashPrivate; |
16 | class QIODevice; |
17 | |
18 | class Q_CORE_EXPORT QCryptographicHash |
19 | { |
20 | Q_GADGET |
21 | public: |
22 | enum Algorithm { |
23 | #ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 |
24 | Md4, |
25 | Md5, |
26 | #endif |
27 | Sha1 = 2, |
28 | #ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 |
29 | Sha224, |
30 | Sha256, |
31 | Sha384, |
32 | Sha512, |
33 | |
34 | Keccak_224 = 7, |
35 | Keccak_256, |
36 | Keccak_384, |
37 | Keccak_512, |
38 | RealSha3_224 = 11, |
39 | RealSha3_256, |
40 | RealSha3_384, |
41 | RealSha3_512, |
42 | # ifndef QT_SHA3_KECCAK_COMPAT |
43 | Sha3_224 = RealSha3_224, |
44 | Sha3_256 = RealSha3_256, |
45 | Sha3_384 = RealSha3_384, |
46 | Sha3_512 = RealSha3_512, |
47 | # else |
48 | Sha3_224 = Keccak_224, |
49 | Sha3_256 = Keccak_256, |
50 | Sha3_384 = Keccak_384, |
51 | Sha3_512 = Keccak_512, |
52 | # endif |
53 | |
54 | Blake2b_160 = 15, |
55 | Blake2b_256, |
56 | Blake2b_384, |
57 | Blake2b_512, |
58 | Blake2s_128, |
59 | Blake2s_160, |
60 | Blake2s_224, |
61 | Blake2s_256, |
62 | #endif |
63 | NumAlgorithms |
64 | }; |
65 | Q_ENUM(Algorithm) |
66 | |
67 | explicit QCryptographicHash(Algorithm method); |
68 | QCryptographicHash(QCryptographicHash &&other) noexcept : d(std::exchange(obj&: other.d, new_val: nullptr)) {} |
69 | ~QCryptographicHash(); |
70 | |
71 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCryptographicHash) |
72 | void swap(QCryptographicHash &other) noexcept { qt_ptr_swap(lhs&: d, rhs&: other.d); } |
73 | |
74 | void reset() noexcept; |
75 | [[nodiscard]] Algorithm algorithm() const noexcept; |
76 | |
77 | #if QT_DEPRECATED_SINCE(6, 4) |
78 | QT_DEPRECATED_VERSION_X_6_4("Use the QByteArrayView overload instead" ) |
79 | void addData(const char *data, qsizetype length); |
80 | #endif |
81 | #if QT_CORE_REMOVED_SINCE(6, 3) |
82 | void addData(const QByteArray &data); |
83 | #endif |
84 | void addData(QByteArrayView data) noexcept; |
85 | bool addData(QIODevice *device); |
86 | |
87 | QByteArray result() const; |
88 | QByteArrayView resultView() const noexcept; |
89 | |
90 | #if QT_CORE_REMOVED_SINCE(6, 3) |
91 | static QByteArray hash(const QByteArray &data, Algorithm method); |
92 | #endif |
93 | static QByteArray hash(QByteArrayView data, Algorithm method); |
94 | static int hashLength(Algorithm method); |
95 | static bool supportsAlgorithm(Algorithm method); |
96 | private: |
97 | Q_DISABLE_COPY(QCryptographicHash) |
98 | QCryptographicHashPrivate *d; |
99 | }; |
100 | |
101 | QT_END_NAMESPACE |
102 | |
103 | #endif |
104 | |