comparison src/common.cpp @ 443:459b140032d4

Introduce random key generator
author Chris Cannam
date Tue, 28 Jun 2011 14:33:50 +0100
parents 498c2ca0b367
children 8506333fe65c
comparison
equal deleted inserted replaced
442:ff2306c67852 443:459b140032d4
38 #include <sys/ioctl.h> 38 #include <sys/ioctl.h>
39 #include <sys/types.h> 39 #include <sys/types.h>
40 #include <sys/stat.h> 40 #include <sys/stat.h>
41 #include <fcntl.h> 41 #include <fcntl.h>
42 #include <signal.h> 42 #include <signal.h>
43 #include <stdio.h>
43 #endif 44 #endif
44 45
45 QString findInPath(QString name, QString installPath, bool executableRequired) 46 QString findInPath(QString name, QString installPath, bool executableRequired)
46 { 47 {
47 bool found = false; 48 bool found = false;
281 // default case: leave alone 282 // default case: leave alone
282 d += s[i]; 283 d += s[i];
283 } 284 }
284 return d; 285 return d;
285 } 286 }
287
288 QByteArray randomKey()
289 {
290 static int len = 16;
291
292 QByteArray ba;
293 ba.resize(len);
294 char *buffer = ba.data();
295
296 #ifdef Q_OS_WIN32
297
298 HCRYPTPROV provider = 0;
299 if (!CryptAcquireContextW(&provider, 0, 0,
300 PROV_RSA_FULL,
301 CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
302 return QByteArray();
303 }
304
305 if (!CryptGenRandom(provider, len, buffer)) {
306 CryptReleaseContext(provider, 0);
307 return QByteArray();
308 }
309
310 CryptReleaseContext(provider, 0);
311
312 #else
313
314 FILE *rf = fopen("/dev/urandom", "r");
315 if (!rf) {
316 return QByteArray();
317 }
318
319 for (int i = 0; i < len; ++i) {
320 buffer[i] = fgetc(rf);
321 }
322
323 fclose(rf);
324
325 #endif
326
327 return ba;
328 }
329