Skip to content

Commit

Permalink
Move random_int() to util.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
computezrmle authored Mar 26, 2024
1 parent b2af307 commit 39a8e8e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
36 changes: 1 addition & 35 deletions lib/crypt_prog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

#include "crypt.h"
#include "md5_file.h"
#include "util.h"

void die(const char* p) {
fprintf(stderr, "Error: %s\n", p);
Expand Down Expand Up @@ -85,41 +86,6 @@ void usage() {
);
}

unsigned int random_int() {
unsigned int n;
#if defined(_WIN32)
#if defined(__CYGWIN32__)
HMODULE hLib=LoadLibrary((const char *)"ADVAPI32.DLL");
#else
HMODULE hLib=LoadLibrary("ADVAPI32.DLL");
#endif
if (!hLib) {
die("Can't load ADVAPI32.DLL");
}
BOOLEAN (APIENTRY *pfn)(void*, ULONG) =
(BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
if (pfn) {
char buff[32];
ULONG ulCbBuff = sizeof(buff);
if(pfn(buff,ulCbBuff)) {
// use buff full of random goop
memcpy(&n,buff,sizeof(n));
}
}
FreeLibrary(hLib);
#else
FILE* f = fopen("/dev/random", "r");
if (!f) {
die("can't open /dev/random\n");
}
if (1 != fread(&n, sizeof(n), 1, f)) {
die("couldn't read from /dev/random\n");
}
fclose(f);
#endif
return n;
}

int main(int argc, char** argv) {
R_RSA_PUBLIC_KEY public_key;
R_RSA_PRIVATE_KEY private_key;
Expand Down
38 changes: 38 additions & 0 deletions lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,41 @@ bool process_exists(int pid) {
}

#endif

unsigned int random_int() {
unsigned int n;
#if defined(_WIN32)
#if defined(__CYGWIN32__)
HMODULE hLib=LoadLibrary((const char *)"ADVAPI32.DLL");
#else
HMODULE hLib=LoadLibrary("ADVAPI32.DLL");
#endif
if (!hLib) {
fprintf(stderr, "Error: can't load ADVAPI32.DLL\n");
exit(2);
}
BOOLEAN (APIENTRY *pfn)(void*, ULONG) =
(BOOLEAN (APIENTRY *)(void*,ULONG))GetProcAddress(hLib,"SystemFunction036");
if (pfn) {
char buff[32];
ULONG ulCbBuff = sizeof(buff);
if(pfn(buff,ulCbBuff)) {
// use buff full of random goop
memcpy(&n,buff,sizeof(n));
}
}
FreeLibrary(hLib);
#else
FILE* f = fopen("/dev/random", "r");
if (!f) {
fprintf(stderr, "Error: can't open /dev/random\n");
exit(2);
}
if (1 != fread(&n, sizeof(n), 1, f)) {
fprintf(stderr, "Error: can't read from /dev/random\n");
exit(2);
}
fclose(f);
#endif
return n;
}
1 change: 1 addition & 0 deletions lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern double dtime();
extern double dday();
extern void boinc_sleep(double);
extern void push_unique(std::string, std::vector<std::string>&);
extern unsigned int random_int();

// NOTE: use #include <functional> to get max,min

Expand Down

0 comments on commit 39a8e8e

Please sign in to comment.