56#include <QCryptographicHash>
59#if defined(Q_OS_WIN32)
73 QByteArray buf(len, 0);
74#if defined(Q_OS_WIN32)
75 static int provider_set = 0;
76 static HCRYPTPROV provider;
78 static QStringList filenames =
79 QStringList() <<
"/dev/srandom" <<
"/dev/urandom" <<
"/dev/random";
83#if defined(Q_OS_WIN32)
85 if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
86 CRYPT_VERIFYCONTEXT)) {
87 if ((
unsigned long)GetLastError() != (
unsigned long)NTE_BAD_KEYSET) {
88 qWarning(
"Can't get CryptoAPI provider.");
94 if (!CryptGenRandom(provider, buf.size(), (BYTE *)buf.data())) {
95 qWarning(
"Can't get entropy from CryptoAPI.");
100 foreach (QString fname, filenames) {
102 if (!file.open(QIODevice::ReadOnly))
107 while (total < buf.size()) {
108 bytes_read = file.read(buf.data()+total, buf.size()-total);
111 else if (bytes_read == 0) {
119 qWarning(
"Can't read from /dev/*random.");
135 cutoff = 0xffffffffu - (0xffffffffu % max);
138 Q_ASSERT(buf.size() ==
sizeof(
quint32));
140 val = *((
quint32 *)buf.constData());
155 for (
int i = 0;
i < len;
i++)
175 QCryptographicHash hash(QCryptographicHash::Sha1);
176 QByteArray tmp = salt.left(8).append(secret.toAscii());
178 if (count > tmp.length()) {
180 count -= tmp.length();
182 hash.addData(tmp.left(count));
187 return hash.result();
quint32 crypto_rand_quint32(quint32 max)
QByteArray crypto_secret_to_key(const QString &secret, const QByteArray &salt, quint8 c)
QString crypto_rand_string(int len)
QByteArray crypto_rand_bytes(int len)