From 8c72dc5907a97fce718f6810d446ecf6ea41b588 Mon Sep 17 00:00:00 2001 From: Liang Qi Date: Wed, 4 Jan 2012 11:15:20 +0100 Subject: Add V3(md5) and V5(sha1) version for DCE in QUuid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the above versions based on RFC4122 standard. Done-with: Hagen Rother Task-number: QTBUG-23071 Change-Id: Ieb90925374d1e3c85011b899b8dd3bb1a608c561 Reviewed-by: João Abecasis Reviewed-by: Denis Dzyubenko --- src/corelib/plugin/quuid.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++-- src/corelib/plugin/quuid.h | 22 +++++++++++++-- 2 files changed, 83 insertions(+), 4 deletions(-) (limited to 'src/corelib/plugin') diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 46589f88bc..e73508fce6 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -133,6 +133,29 @@ bool _q_uuidFromHex(const Char *&src, uint &d1, ushort &d2, ushort &d3, uchar (& } #endif +static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCryptographicHash::Algorithm algorithm, int version) +{ + QByteArray hashResult; + + // create a scope so later resize won't reallocate + { + QCryptographicHash hash(algorithm); + hash.addData(ns.toRfc4122()); + hash.addData(baseData); + hashResult = hash.result(); + } + hashResult.resize(16); // Sha1 will be too long + + QUuid result = QUuid::fromRfc4122(hashResult); + + result.data3 &= 0x0FFF; + result.data3 |= (version << 12); + result.data4[0] &= 0x3F; + result.data4[0] |= 0x80; + + return result; +} + /*! \class QUuid \brief The QUuid class stores a Universally Unique Identifier (UUID). @@ -231,7 +254,7 @@ bool _q_uuidFromHex(const Char *&src, uint &d1, ushort &d2, ushort &d3, uchar (& \o 0 \o 1 \o 1 - \o Name + \o Md5(Name) \row \o 0 @@ -240,6 +263,13 @@ bool _q_uuidFromHex(const Char *&src, uint &d1, ushort &d2, ushort &d3, uchar (& \o 0 \o Random + \row + \o 0 + \o 1 + \o 0 + \o 1 + \o Sha1 + \endtable The field layouts for the DCE versions listed in the table above @@ -385,8 +415,39 @@ QUuid::QUuid(const QByteArray &text) return; } } + #endif +/*! + \since 5.0 + \fn QUuid::createUuidV3() + + This functions returns a new UUID with variant QUuid::DCE and version QUuid::MD5. + \a ns is the namespace and \a name is the name as described by RFC 4122. + + \sa variant(), version(), createUuidV5() +*/ + +/*! + \since 5.0 + \fn QUuid::createUuidV5() + + This functions returns a new UUID with variant QUuid::DCE and version QUuid::SHA1. + \a ns is the namespace and \a name is the name as described by RFC 4122. + + \sa variant(), version(), createUuidV3() +*/ + +QUuid QUuid::createUuidV3(const QUuid &ns, const QByteArray &baseData) +{ + return createFromName(ns, baseData, QCryptographicHash::Md5, 3); +} + +QUuid QUuid::createUuidV5(const QUuid &ns, const QByteArray &baseData) +{ + return createFromName(ns, baseData, QCryptographicHash::Sha1, 5); +} + /*! Creates a QUuid object from the binary representation of the UUID, as specified by RFC 4122 section 4.1.2. See toRfc4122() for a further @@ -731,7 +792,7 @@ QUuid::Version QUuid::version() const if (isNull() || (variant() != DCE) || ver < Time - || ver > Random) + || ver > Sha1) return VerUnknown; return ver; } diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h index 2eec1575e5..9efb2ba37c 100644 --- a/src/corelib/plugin/quuid.h +++ b/src/corelib/plugin/quuid.h @@ -43,6 +43,7 @@ #define QUUID_H #include +#include QT_BEGIN_HEADER @@ -79,8 +80,10 @@ public: VerUnknown =-1, Time = 1, // 0 0 0 1 EmbeddedPOSIX = 2, // 0 0 1 0 - Name = 3, // 0 0 1 1 - Random = 4 // 0 1 0 0 + Md5 = 3, // 0 0 1 1 + Name = Md5, + Random = 4, // 0 1 0 0 + Sha1 = 5 // 0 1 0 1 }; QUuid() @@ -173,6 +176,21 @@ public: } #endif static QUuid createUuid(); + static QUuid createUuidV3(const QUuid &ns, const QByteArray &baseData); + static QUuid createUuidV5(const QUuid &ns, const QByteArray &baseData); +#ifndef QT_NO_QUUID_STRING + static inline QUuid createUuidV3(const QUuid &ns, const QString &baseData) + { + return QUuid::createUuidV3(ns, baseData.toUtf8()); + } + + static inline QUuid createUuidV5(const QUuid &ns, const QString &baseData) + { + return QUuid::createUuidV5(ns, baseData.toUtf8()); + } + +#endif + QUuid::Variant variant() const; QUuid::Version version() const; -- cgit v1.2.3