Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcryptographichash.h
Go to the documentation of this file.
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// Qt-Security score:critical reason:cryptography
6
7#ifndef QCRYPTOGRAPHICHASH_H
8#define QCRYPTOGRAPHICHASH_H
9
10#include <QtCore/qbytearray.h>
11#include <QtCore/qobjectdefs.h>
12#include <QtCore/qspan.h>
13
14QT_BEGIN_NAMESPACE
15
16
17class QCryptographicHashPrivate;
18class QIODevice;
19
20class Q_CORE_EXPORT QCryptographicHash
21{
22 Q_GADGET
23public:
24 enum Algorithm {
25 Md4,
26 Md5,
27 Sha1 = 2,
28 Sha224,
29 Sha256,
30 Sha384,
31 Sha512,
32
33 Keccak_224 = 7,
34 Keccak_256,
35 Keccak_384,
36 Keccak_512,
37 RealSha3_224 = 11,
38 RealSha3_256,
39 RealSha3_384,
40 RealSha3_512,
41# ifndef QT_SHA3_KECCAK_COMPAT
42 Sha3_224 = RealSha3_224,
43 Sha3_256 = RealSha3_256,
44 Sha3_384 = RealSha3_384,
45 Sha3_512 = RealSha3_512,
46# else
47 Sha3_224 = Keccak_224,
48 Sha3_256 = Keccak_256,
49 Sha3_384 = Keccak_384,
50 Sha3_512 = Keccak_512,
51# endif
52
53 Blake2b_160 = 15,
54 Blake2b_256,
55 Blake2b_384,
56 Blake2b_512,
57 Blake2s_128,
58 Blake2s_160,
59 Blake2s_224,
60 Blake2s_256,
61 NumAlgorithms
62 };
63 Q_ENUM(Algorithm)
64
65 explicit QCryptographicHash(Algorithm method);
66 QCryptographicHash(QCryptographicHash &&other) noexcept : d(std::exchange(other.d, nullptr)) {}
67 ~QCryptographicHash();
68
69 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCryptographicHash)
70 void swap(QCryptographicHash &other) noexcept { qt_ptr_swap(d, other.d); }
71
72 void reset() noexcept;
73 [[nodiscard]] Algorithm algorithm() const noexcept;
74
75#if QT_DEPRECATED_SINCE(6, 4)
76 QT_DEPRECATED_VERSION_X_6_4("Use the QByteArrayView overload instead")
77 void addData(const char *data, qsizetype length);
78#endif
79#if QT_CORE_REMOVED_SINCE(6, 3)
80 void addData(const QByteArray &data);
81#endif
82 void addData(QByteArrayView data) noexcept;
83 bool addData(QIODevice *device);
84
85 QByteArray result() const;
86 QByteArrayView resultView() const noexcept;
87
88#if QT_CORE_REMOVED_SINCE(6, 3)
89 static QByteArray hash(const QByteArray &data, Algorithm method);
90#endif
91 static QByteArray hash(QByteArrayView data, Algorithm method);
92
93 static QByteArrayView hashInto(QSpan<char> buffer, QByteArrayView data, Algorithm method) noexcept
94 { return hashInto(as_writable_bytes(buffer), {&data, 1}, method); }
95 static QByteArrayView hashInto(QSpan<uchar> buffer, QByteArrayView data, Algorithm method) noexcept
96 { return hashInto(as_writable_bytes(buffer), {&data, 1}, method); }
97 static QByteArrayView hashInto(QSpan<std::byte> buffer, QByteArrayView data, Algorithm method) noexcept
98 { return hashInto(buffer, {&data, 1}, method); }
99 static QByteArrayView hashInto(QSpan<char> buffer, QSpan<const QByteArrayView> data, Algorithm method) noexcept
100 { return hashInto(as_writable_bytes(buffer), data, method); }
101 static QByteArrayView hashInto(QSpan<uchar> buffer, QSpan<const QByteArrayView> data, Algorithm method) noexcept
102 { return hashInto(as_writable_bytes(buffer), data, method); }
103 static QByteArrayView hashInto(QSpan<std::byte> buffer, QSpan<const QByteArrayView> data, Algorithm method) noexcept;
104
105 static int hashLength(Algorithm method);
106 static bool supportsAlgorithm(Algorithm method);
107private:
108 Q_DISABLE_COPY(QCryptographicHash)
109 QCryptographicHashPrivate *d;
110};
111
112QT_END_NAMESPACE
113
114#endif