From f583f1a71459f413f9f869b8f1616722054dbea8 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Mon, 10 Feb 2020 22:16:12 +0100 Subject: utils: Generate UUIDs for stanza hashes by default The QXmppUtils::generateStanzaHash() generates UUIDs by default now. UUIDs are not generated, if the default parameter is changed to a different value (!= 36). The behaviour is not changed for other values than 36. This way all users of QXmpp will automatically start to use UUIDs, if they use the generateStanzaHash() method. --- src/base/QXmppUtils.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/base/QXmppUtils.cpp') diff --git a/src/base/QXmppUtils.cpp b/src/base/QXmppUtils.cpp index 7c5f631e..5d09c1c7 100644 --- a/src/base/QXmppUtils.cpp +++ b/src/base/QXmppUtils.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include // adapted from public domain source by Ross Williams and Eric Durbin @@ -291,12 +292,40 @@ QByteArray QXmppUtils::generateRandomBytes(int length) return bytes; } +/// +/// Creates a new stanza id in the UUID format. +/// +/// \since QXmpp 1.3 +/// +QString QXmppUtils::generateStanzaUuid() +{ +#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) + return QUuid::createUuid().toString(QUuid::WithoutBraces); +#else + return QUuid::createUuid().toString().mid(1, 36); +#endif +} + +/// /// Returns a random alphanumerical string of the specified size. /// +/// Since QXmpp 1.3 this will generate a UUID, if the specified \p length is 36 +/// which is also the new default value. The returned string is still 36 +/// characters long, but will contain dashes (as specified in the UUID format). +/// +/// \note It is recommended to use UUIDs for cases where IDs must be unique and +/// are possibly stored permanently. This can be done using +/// QXmppUtils::generateStanzaUuid(). However, since that function is only +/// available since QXmpp 1.3, you may also want to continue to use this +/// function because of compatibility reasons. +/// /// \param length - +/// QString QXmppUtils::generateStanzaHash(int length) { + if (length == 36) + return QXmppUtils::generateStanzaUuid(); + const QString somechars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int N = somechars.size(); QString hashResult; -- cgit v1.2.3