# HG changeset patch # User Chris Cannam # Date 1309268030 -3600 # Node ID 459b140032d4d25e6de629f263e48df21f7eeae7 # Parent ff2306c678528fcf83820bbeb038afcd1f3808f5 Introduce random key generator diff -r ff2306c67852 -r 459b140032d4 src/common.cpp --- a/src/common.cpp Tue Jun 28 14:33:44 2011 +0100 +++ b/src/common.cpp Tue Jun 28 14:33:50 2011 +0100 @@ -40,6 +40,7 @@ #include #include #include +#include #endif QString findInPath(QString name, QString installPath, bool executableRequired) @@ -283,3 +284,46 @@ } return d; } + +QByteArray randomKey() +{ + static int len = 16; + + QByteArray ba; + ba.resize(len); + char *buffer = ba.data(); + +#ifdef Q_OS_WIN32 + + HCRYPTPROV provider = 0; + if (!CryptAcquireContextW(&provider, 0, 0, + PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { + return QByteArray(); + } + + if (!CryptGenRandom(provider, len, buffer)) { + CryptReleaseContext(provider, 0); + return QByteArray(); + } + + CryptReleaseContext(provider, 0); + +#else + + FILE *rf = fopen("/dev/urandom", "r"); + if (!rf) { + return QByteArray(); + } + + for (int i = 0; i < len; ++i) { + buffer[i] = fgetc(rf); + } + + fclose(rf); + +#endif + + return ba; +} + diff -r ff2306c67852 -r 459b140032d4 src/common.h --- a/src/common.h Tue Jun 28 14:33:44 2011 +0100 +++ b/src/common.h Tue Jun 28 14:33:50 2011 +0100 @@ -20,11 +20,6 @@ #include -#define MY_ICON_SIZE 32 -//!!!: -#define REPOMENU_TITLE "Repository actions" -#define WORKFOLDERMENU_TITLE "Workfolder actions" - extern QString findInPath(QString name, QString installPath, bool executable); extern QString getSystem(); @@ -64,6 +59,11 @@ QString xmlEncode(QString); QString uniDecode(QString); + +/** + * Generate a 16-byte random key using urandom or equivalent + */ +QByteArray randomKey(); #endif //COMMON_H