aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2010-09-01 11:58:31 +0000
committerJeremy Lainé <jeremy.laine@m4x.org>2010-09-01 11:58:31 +0000
commitcd218ff692df40b0324ea6834eb4924293970de0 (patch)
treea9b0cc13722e3a149ee14c59e58e6e144faf5794 /src
parent4cc43f4f91ea32eee2537184d78c98e4d8755076 (diff)
downloadqxmpp-cd218ff692df40b0324ea6834eb4924293970de0.tar.gz
unify random number generation
Diffstat (limited to 'src')
-rw-r--r--src/QXmppSaslAuth.cpp4
-rw-r--r--src/QXmppStun.cpp14
-rw-r--r--src/QXmppUtils.cpp35
-rw-r--r--src/QXmppUtils.h1
4 files changed, 36 insertions, 18 deletions
diff --git a/src/QXmppSaslAuth.cpp b/src/QXmppSaslAuth.cpp
index 6b642f13..89f4edd4 100644
--- a/src/QXmppSaslAuth.cpp
+++ b/src/QXmppSaslAuth.cpp
@@ -106,9 +106,7 @@ void QXmppSaslDigestMd5::setPassword(const QByteArray &password)
QByteArray QXmppSaslDigestMd5::generateNonce()
{
- QByteArray nonce(32, 'm');
- for(int n = 0; n < nonce.size(); ++n)
- nonce[n] = (char)(256.0*qrand()/(RAND_MAX+1.0));
+ QByteArray nonce = generateRandomBytes(32);
// The random data can the '=' char is not valid as it is a delimiter,
// so to be safe, base64 the nonce
diff --git a/src/QXmppStun.cpp b/src/QXmppStun.cpp
index 092484b4..283d0ba8 100644
--- a/src/QXmppStun.cpp
+++ b/src/QXmppStun.cpp
@@ -124,14 +124,6 @@ static void encodeString(QDataStream &stream, quint16 type, const QString &strin
}
}
-static QByteArray randomByteArray(int length)
-{
- QByteArray result(length, 0);
- for (int i = 0; i < length; ++i)
- result[i] = quint8(qrand() % 255);
- return result;
-}
-
/// Constructs a new QXmppStunMessage.
QXmppStunMessage::QXmppStunMessage()
@@ -653,7 +645,7 @@ QXmppStunSocket::Pair::Pair()
{
// FIXME : calculate priority
priority = 1862270975;
- transaction = randomByteArray(ID_SIZE);
+ transaction = generateRandomBytes(ID_SIZE);
}
QString QXmppStunSocket::Pair::toString() const
@@ -891,7 +883,7 @@ void QXmppStunSocket::setStunServer(const QHostAddress &host, quint16 port)
{
m_stunHost = host;
m_stunPort = port;
- m_stunId = randomByteArray(ID_SIZE);
+ m_stunId = generateRandomBytes(ID_SIZE);
}
void QXmppStunSocket::readyRead()
@@ -1046,7 +1038,7 @@ void QXmppStunSocket::readyRead()
#if 0
// send a binding indication
QXmppStunMessage indication;
- indication.setId(randomByteArray(ID_SIZE));
+ indication.setId(generateRandomBytes(ID_SIZE));
indication.setType(BindingIndication);
m_socket->writeStun(indication, pair);
#endif
diff --git a/src/QXmppUtils.cpp b/src/QXmppUtils.cpp
index fcf12b1d..8b560cc3 100644
--- a/src/QXmppUtils.cpp
+++ b/src/QXmppUtils.cpp
@@ -216,14 +216,41 @@ QByteArray generateHmacSha1(const QByteArray &key, const QByteArray &text)
return generateHmac(QCryptographicHash::Sha1, key, text);
}
+/// Generates a random integer x between 0 and N-1.
+///
+/// \param N
+
+static int generateRandomInteger(int N)
+{
+ Q_ASSERT(N > 0 && N <= RAND_MAX);
+ int val;
+ while (N <= (val = qrand() / (RAND_MAX/N)));
+ return val;
+}
+
+/// Returns a random byte array of the specified size.
+///
+/// \param length
+
+QByteArray generateRandomBytes(int length)
+{
+ QByteArray bytes(length, 'm');
+ for (int i = 0; i < length; ++i)
+ bytes[i] = (char)generateRandomInteger(256);
+ return bytes;
+}
+
+/// Returns a random alphanumerical string of the specified size.
+///
+/// \param length
+
QString generateStanzaHash(int length)
{
- QString somechars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ const QString somechars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ const int N = somechars.size();
QString hashResult;
for ( int idx = 0; idx < length; ++idx )
- {
- hashResult += somechars[(qrand() % 61)];
- }
+ hashResult += somechars[generateRandomInteger(N)];
return hashResult;
}
diff --git a/src/QXmppUtils.h b/src/QXmppUtils.h
index fce000b9..1757238d 100644
--- a/src/QXmppUtils.h
+++ b/src/QXmppUtils.h
@@ -50,6 +50,7 @@ QString jidToBareJid(const QString& jid);
quint32 generateCrc32(const QByteArray &input);
QByteArray generateHmacMd5(const QByteArray &key, const QByteArray &text);
QByteArray generateHmacSha1(const QByteArray &key, const QByteArray &text);
+QByteArray generateRandomBytes(int length);
QString generateStanzaHash(int length=32);
void helperToXmlAddAttribute(QXmlStreamWriter* stream, const QString& name,