From 39a8e8e440d0fb1ee01b1c89afe85314d05ddc3a Mon Sep 17 00:00:00 2001 From: computezrmle <57127745+computezrmle@users.noreply.github.com> Date: Tue, 26 Mar 2024 22:13:27 +0100 Subject: [PATCH] Move random_int() to util.cpp --- lib/crypt_prog.cpp | 36 +----------------------------------- lib/util.cpp | 38 ++++++++++++++++++++++++++++++++++++++ lib/util.h | 1 + 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/lib/crypt_prog.cpp b/lib/crypt_prog.cpp index 5e4d5878b08..8d88c6ae611 100644 --- a/lib/crypt_prog.cpp +++ b/lib/crypt_prog.cpp @@ -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); @@ -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; diff --git a/lib/util.cpp b/lib/util.cpp index 2af4b0fb945..b069c0ed1a5 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -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; +} diff --git a/lib/util.h b/lib/util.h index fd7f6bedf60..f95361cf3d0 100644 --- a/lib/util.h +++ b/lib/util.h @@ -30,6 +30,7 @@ extern double dtime(); extern double dday(); extern void boinc_sleep(double); extern void push_unique(std::string, std::vector&); +extern unsigned int random_int(); // NOTE: use #include to get max,min