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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtbase/src/corelib/tools/qcryptographichash.h