KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
cryptoutil.h
1/**
2 * CryptoUtil
3 *
4 * Some static helper methods for MD5 hashes (to and from strings).
5 *
6 * It is basically a front-end for QCryptographicHash functions.
7 *
8 * Stephen Punak, February 17 2021
9 */
10#ifndef CRYPTOUTIL_H
11#define CRYPTOUTIL_H
12
13#include <QByteArray>
14#include <QString>
15#include <QStringList>
16#include <Kanoop/kanoopcommon.h>
17
18/**
19 * @brief Static helper methods for computing MD5 hashes as byte arrays or hex strings.
20 */
21class KANOOP_EXPORT CryptoUtil
22{
23public:
24 /**
25 * @brief Compute the MD5 hash of a file's contents.
26 * @param filename Path to the file
27 * @return Raw MD5 hash bytes, or empty array on error
28 */
29 static QByteArray fileMd5(const QString& filename);
30
31 /**
32 * @brief Compute the MD5 hash of a file's contents as a hex string.
33 * @param filename Path to the file
34 * @return Hex-encoded MD5 string, or empty string on error
35 */
36 static QString fileMd5String(const QString& filename);
37
38 /**
39 * @brief Compute the MD5 hash of a byte array.
40 * @param of Input data
41 * @return Raw MD5 hash bytes
42 */
43 static QByteArray md5(const QByteArray& of);
44
45 /**
46 * @brief Compute the MD5 hash of a byte array as a 64-bit unsigned integer.
47 * @param of Input data
48 * @return First 8 bytes of the MD5 hash interpreted as uint64_t
49 */
50 static uint64_t md5UInt64(const QByteArray& of);
51
52 /**
53 * @brief Compute the MD5 hash of a string as a hex string.
54 * @param of Input string
55 * @return Hex-encoded MD5 string
56 */
57 static QString md5String(const QString& of);
58
59 /**
60 * @brief Compute the MD5 hash of a byte array as a hex string.
61 * @param of Input data
62 * @return Hex-encoded MD5 string
63 */
64 static QString md5String(const QByteArray& of);
65
66 /**
67 * @brief Compute the MD5 hash of a joined string list as a hex string.
68 * @param of List of strings to hash (concatenated)
69 * @return Hex-encoded MD5 string
70 */
71 static QString md5String(const QStringList& of);
72
73 /**
74 * @brief Compute the MD5 hash of a string list as raw bytes.
75 * @param of List of strings to hash (concatenated)
76 * @return Raw MD5 hash bytes
77 */
78 static QByteArray md5(const QStringList& of);
79
80 /**
81 * @brief Compute the SHA-256 hash of a file's contents.
82 * @param filename Path to the file
83 * @return Raw SHA-256 hash bytes, or empty array on error
84 */
85 static QByteArray fileSha256(const QString& filename);
86
87 /**
88 * @brief Compute the SHA-256 hash of a file's contents as a hex string.
89 * @param filename Path to the file
90 * @return Hex-encoded SHA-256 string, or empty string on error
91 */
92 static QString fileSha256String(const QString& filename);
93
94 /**
95 * @brief Compute the SHA-256 hash of a byte array.
96 * @param of Input data
97 * @return Raw SHA-256 hash bytes
98 */
99 static QByteArray sha256(const QByteArray& of);
100
101 /**
102 * @brief Compute the SHA-256 hash of a string as a hex string.
103 * @param of Input string
104 * @return Hex-encoded SHA-256 string
105 */
106 static QString sha256String(const QString& of);
107
108 /**
109 * @brief Compute the SHA-256 hash of a byte array as a hex string.
110 * @param of Input data
111 * @return Hex-encoded SHA-256 string
112 */
113 static QString sha256String(const QByteArray& of);
114
115 /**
116 * @brief Compute the SHA-256 hash of a joined string list as a hex string.
117 * @param of List of strings to hash (concatenated)
118 * @return Hex-encoded SHA-256 string
119 */
120 static QString sha256String(const QStringList& of);
121
122 /**
123 * @brief Compute the SHA-256 hash of a string list as raw bytes.
124 * @param of List of strings to hash (concatenated)
125 * @return Raw SHA-256 hash bytes
126 */
127 static QByteArray sha256(const QStringList& of);
128
129private:
130 /** @brief Convert a raw hash byte array to a lowercase hex string. */
131 static QString toHashString(const QByteArray& buffer);
132};
133
134#endif // CRYPTOUTIL_H
CryptoUtil.
Definition cryptoutil.h:22
static QString md5String(const QByteArray &of)
Compute the MD5 hash of a byte array as a hex string.
static QString md5String(const QStringList &of)
Compute the MD5 hash of a joined string list as a hex string.
static QByteArray sha256(const QByteArray &of)
Compute the SHA-256 hash of a byte array.
static QString sha256String(const QByteArray &of)
Compute the SHA-256 hash of a byte array as a hex string.
static QString sha256String(const QStringList &of)
Compute the SHA-256 hash of a joined string list as a hex string.
static QString fileMd5String(const QString &filename)
Compute the MD5 hash of a file's contents as a hex string.
static QByteArray fileSha256(const QString &filename)
Compute the SHA-256 hash of a file's contents.
static QString fileSha256String(const QString &filename)
Compute the SHA-256 hash of a file's contents as a hex string.
static QByteArray md5(const QByteArray &of)
Compute the MD5 hash of a byte array.
static QByteArray fileMd5(const QString &filename)
Compute the MD5 hash of a file's contents.
static QString md5String(const QString &of)
Compute the MD5 hash of a string as a hex string.
static QString sha256String(const QString &of)
Compute the SHA-256 hash of a string as a hex string.
static uint64_t md5UInt64(const QByteArray &of)
Compute the MD5 hash of a byte array as a 64-bit unsigned integer.
static QByteArray md5(const QStringList &of)
Compute the MD5 hash of a string list as raw bytes.
static QByteArray sha256(const QStringList &of)
Compute the SHA-256 hash of a string list as raw bytes.