changeset 443:459b140032d4

Introduce random key generator
author Chris Cannam
date Tue, 28 Jun 2011 14:33:50 +0100
parents ff2306c67852
children 66ec8b2ee946
files src/common.cpp src/common.h
diffstat 2 files changed, 49 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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 <sys/stat.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <stdio.h>
 #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;
+}
+
--- 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 <QString>
 
-#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