Try to use truly random data to initialize the random number generator

Fixed: crash when closing the PasswordDlg by clicking on the x
Fixed: -cfg parameter not working
Fixed: does not compile for Windows
Fixed some compiler warnings

git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@176 b624d157-de02-0410-bad0-e51aec6abb33
This commit is contained in:
sniperbeamer
2008-03-08 14:18:32 +00:00
parent e9a85f91fc
commit b8f59f9f04
12 changed files with 98 additions and 85 deletions

View File

@@ -19,29 +19,44 @@
***************************************************************************/
#include <iostream>
#include <fstream>
#include <QDateTime>
#include <QObject>
#include "random.h"
#if defined(Q_WS_WIN)
#include <QSysInfo>
#include <QMessageBox>
#include <windows.h>
#endif
using namespace std;
void getRandomBytes(void* buffer,int NumBlocks,int BlockSize,bool Strong){
FILE *dev_random;
if(Strong){
dev_random = fopen("/dev/random","r");}
else{
dev_random = fopen("/dev/urandom","r");}
if (dev_random==NULL){
srand(QTime(0,0,0).secsTo(QTime::currentTime()));
for(int i=0;i<NumBlocks*BlockSize;i++){
quint8 rnd=rand()%256;
((quint8*)buffer)[i]=rnd;
void getRandomBytes(void* buffer,int NumBlocks){
#if defined(Q_WS_WIN)
// RtlGenRandom
if (QSysInfo::WindowsVersion>=QSysInfo::WV_XP){
bool success=false;
HMODULE hLib=LoadLibraryA("ADVAPI32.DLL");
if (hLib) {
BOOLEAN (APIENTRY *pfn)(void*, ULONG) = (BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
if (pfn && pfn(buffer,NumBlocks)) {
success=true;
}
FreeLibrary(hLib);
}
if (success)
return;
}
#else
FILE* dev_random = fopen("/dev/random","r");
if (dev_random){
size_t bytesRead = fread(buffer,1,NumBlocks,dev_random);
fclose(dev_random);
if (bytesRead==NumBlocks)
return;
}
#endif
srand(time(NULL));
for(int i=0;i<NumBlocks;i++){
((quint8*)buffer)[i] = (quint8) (rand()%256);
}
return;
}
else{
fread(buffer,BlockSize,NumBlocks,dev_random);
fclose(dev_random);
return;
}
}

View File

@@ -20,7 +20,6 @@
#ifndef RANDOM_H_
#define RANDOM_H_
extern void getRandomBytes(void* buffer,int NumBlocks,int BlockSize=1,bool Strong=false);
extern void getRandomBytes(void* buffer,int NumBlocks);
#endif
#endif