aboutsummaryrefslogtreecommitdiff
path: root/src/base/QXmppUtils.cpp
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2022-08-12 18:00:10 +0200
committerLinus Jahn <lnj@kaidan.im>2022-08-13 15:55:03 +0200
commitec0669845b9072ea6cdc0fefb66f1d07511386a4 (patch)
tree47afedba2c961dc9b28ef1e387b1a168735ca868 /src/base/QXmppUtils.cpp
parent80ddd9d5683b023e7ae20557118c839e59069d83 (diff)
downloadqxmpp-ec0669845b9072ea6cdc0fefb66f1d07511386a4.tar.gz
Utils_p: Add functions to generate random QByteArray
Co-authored-by: Melvin Keskin <melvo@olomono.de>
Diffstat (limited to 'src/base/QXmppUtils.cpp')
-rw-r--r--src/base/QXmppUtils.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/base/QXmppUtils.cpp b/src/base/QXmppUtils.cpp
index 1d99a87f..462d19fb 100644
--- a/src/base/QXmppUtils.cpp
+++ b/src/base/QXmppUtils.cpp
@@ -6,6 +6,7 @@
#include "QXmppUtils.h"
#include "QXmppLogger.h"
+#include "QXmppUtils_p.h"
#include <QBuffer>
#include <QByteArray>
@@ -322,3 +323,69 @@ void helperToXmlAddTextElement(QXmlStreamWriter *stream, const QString &name,
else
stream->writeEmptyElement(name);
}
+
+/// \cond
+
+//
+// Generates a random count of random bytes.
+//
+// \param minimumByteCount minimum count of bytes to generate
+// \param maximumByteCount maximum count of bytes to generate
+//
+// \return the generated bytes
+//
+QByteArray QXmpp::Private::generateRandomBytes(uint32_t minimumByteCount, uint32_t maximumByteCount)
+{
+#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
+ const auto byteCount = QRandomGenerator::system()->bounded(minimumByteCount, maximumByteCount);
+#else
+ const auto byteCount = (qrand() % (maximumByteCount - minimumByteCount)) + minimumByteCount;
+#endif
+ QByteArray bytes;
+ bytes.resize(byteCount);
+ generateRandomBytes(reinterpret_cast<uint8_t *>(bytes.data()), byteCount);
+
+ return bytes;
+}
+
+//
+// Generates random bytes.
+//
+// \param bytes generated bytes
+// \param byteCount count of bytes to generate
+//
+void QXmpp::Private::generateRandomBytes(uint8_t *bytes, uint32_t byteCount)
+{
+ // QRandomGenerator does not provide a random generation of single bytes. Thus, this approach
+ // fills an array with unsigned integers until no additional integer fits into the array. If
+ // byteCount is no multiple of intSize, the remaining bytes need to be filled separately.
+ constexpr uint32_t intSize = sizeof(uint32_t);
+ auto intCount = byteCount / intSize;
+
+#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
+ auto *randomGenerator = QRandomGenerator::system();
+
+ // Fill the space with intCount unsigned integers.
+ if (intCount) {
+ randomGenerator->fillRange(reinterpret_cast<uint32_t *>(bytes), intCount);
+ }
+
+ // Fill the remaining space with single bytes.
+ for (size_t i = byteCount - byteCount % intSize; i < byteCount; i++) {
+ bytes[i] = randomGenerator->bounded(std::numeric_limits<uint8_t>::max() + 1);
+ }
+#else
+ // Fill the range with intCount unsigned integers.
+ for (size_t i = 0; i < intCount; i++) {
+ reinterpret_cast<int *>(bytes)[i] = qrand();
+ }
+
+ // Fill the remaining space with single bytes.
+ for (size_t i = byteCount - byteCount % intSize; i < byteCount; i++) {
+ auto rand = qrand();
+ bytes[i] = *reinterpret_cast<uint *>(&rand) % (std::numeric_limits<uint8_t>::max() + 1);
+ }
+#endif
+}
+
+/// \endcond