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