本文整理汇总了C++中RAND_add函数的典型用法代码示例。如果您正苦于以下问题:C++ RAND_add函数的具体用法?C++ RAND_add怎么用?C++ RAND_add使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RAND_add函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: RandAddSeedPerfmon
void RandAddSeedPerfmon()
{
RandAddSeed();
// This can take up to 2 seconds, so only do it every 10 minutes
static int64 nLastPerfmon;
if (GetTime() < nLastPerfmon + 10 * 60)
return;
nLastPerfmon = GetTime();
#ifdef __WXMSW__
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom
// Seed with the entire set of perfmon data
unsigned char pdata[250000];
memset(pdata, 0, sizeof(pdata));
unsigned long nSize = sizeof(pdata);
long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
RegCloseKey(HKEY_PERFORMANCE_DATA);
if (ret == ERROR_SUCCESS)
{
RAND_add(pdata, nSize, nSize/100.0);
memset(pdata, 0, nSize);
printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
}
#endif
}
示例2: RandAddSeed
void RandAddSeed()
{
// Seed with CPU performance counter
int64 nCounter = GetPerformanceCounter();
RAND_add(&nCounter, sizeof(nCounter), 1.5);
memset(&nCounter, 0, sizeof(nCounter));
}
示例3: RAND_poll
int RAND_poll(void)
{
unsigned long l;
#ifdef HAVE_GETPID
pid_t curr_pid = getpid();
#endif
#ifdef DEVRANDOM
FILE *fh;
#endif
#ifdef DEVRANDOM
/* Use a random entropy pool device. Linux, FreeBSD and OpenBSD
* have this. Use /dev/urandom if you can as /dev/random may block
* if it runs out of random entries. */
if ((fh = fopen(DEVRANDOM, "r")) != NULL)
{
unsigned char tmpbuf[ENTROPY_NEEDED];
int n;
setvbuf(fh, NULL, _IONBF, 0);
n=fread((unsigned char *)tmpbuf,1,ENTROPY_NEEDED,fh);
fclose(fh);
RAND_add(tmpbuf,sizeof tmpbuf,n);
memset(tmpbuf,0,n);
}
#endif
/* put in some default random data, we need more than just this */
#ifdef HAVE_GETPID
l=curr_pid;
RAND_add(&l,sizeof(l),0);
#endif
#ifdef HAVE_GETUID
l=getuid();
RAND_add(&l,sizeof(l),0);
#endif
l=time(NULL);
RAND_add(&l,sizeof(l),0);
#ifdef DEVRANDOM
return 1;
#else
return 0;
#endif
}
示例4: seed_something
static int
seed_something(void)
{
#ifndef NO_RANDFILE
char buf[1024], seedfile[256];
/* If there is a seed file, load it. But such a file cannot be trusted,
so use 0 for the entropy estimate */
if (RAND_file_name(seedfile, sizeof(seedfile))) {
int fd;
fd = open(seedfile, O_RDONLY | O_BINARY | O_CLOEXEC);
if (fd >= 0) {
ssize_t ret;
rk_cloexec(fd);
ret = read(fd, buf, sizeof(buf));
if (ret > 0)
RAND_add(buf, ret, 0.0);
close(fd);
} else
seedfile[0] = '\0';
} else
seedfile[0] = '\0';
#endif
/* Calling RAND_status() will try to use /dev/urandom if it exists so
we do not have to deal with it. */
if (RAND_status() != 1) {
#if defined(HAVE_RAND_EGD)
krb5_context context;
const char *p;
#ifndef OPENSSL_NO_EGD
/* Try using egd */
if (!krb5_init_context(&context)) {
p = krb5_config_get_string(context, NULL, "libdefaults",
"egd_socket", NULL);
if (p != NULL)
RAND_egd_bytes(p, ENTROPY_NEEDED);
krb5_free_context(context);
}
#endif
#else
/* TODO: Once a Windows CryptoAPI RAND method is defined, we
can use that and failover to another method. */
#endif
}
if (RAND_status() == 1) {
#ifndef NO_RANDFILE
/* Update the seed file */
if (seedfile[0])
RAND_write_file(seedfile);
#endif
return 0;
} else
return -1;
}
示例5: ossl_rand_add
/*
* call-seq:
* add(str, entropy) -> self
*
*/
static VALUE
ossl_rand_add(VALUE self, VALUE str, VALUE entropy)
{
StringValue(str);
RAND_add(RSTRING_PTR(str), RSTRING_LEN(str), NUM2DBL(entropy));
return self;
}
示例6: rand_add
static int rand_add(lua_State *L)
{
size_t num;
const void *buf = luaL_checklstring(L, 1, &num);
double entropy = luaL_optnumber(L, 2, num);
RAND_add(buf, (int)num, entropy);
return 0;
}
示例7: RAND_poll
int RAND_poll(void)
{
unsigned long Time=(unsigned long)TINYCLR_SSL_TIME(NULL);
RAND_add(&Time,sizeof(Time),ENTROPY_NEEDED);
return 1;
}
示例8: RAND_poll
int RAND_poll(void)
{
MEMORYSTATUS mst;
# ifndef RAND_WINDOWS_USE_BCRYPT
HCRYPTPROV hProvider;
# endif
DWORD w;
BYTE buf[64];
# ifdef RAND_WINDOWS_USE_BCRYPT
if (BCryptGenRandom(NULL, buf, (ULONG)sizeof(buf), BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS) {
RAND_add(buf, sizeof(buf), sizeof(buf));
}
# else
/* poll the CryptoAPI PRNG */
/* The CryptoAPI returns sizeof(buf) bytes of randomness */
if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) {
RAND_add(buf, sizeof(buf), sizeof(buf));
}
CryptReleaseContext(hProvider, 0);
}
/* poll the Pentium PRG with CryptoAPI */
if (CryptAcquireContextW(&hProvider, NULL, INTEL_DEF_PROV, PROV_INTEL_SEC, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) {
RAND_add(buf, sizeof(buf), sizeof(buf));
}
CryptReleaseContext(hProvider, 0);
}
# endif
/* timer data */
readtimer();
/* memory usage statistics */
GlobalMemoryStatus(&mst);
RAND_add(&mst, sizeof(mst), 1);
/* process ID */
w = GetCurrentProcessId();
RAND_add(&w, sizeof(w), 1);
return (1);
}
示例9: px_add_entropy
int
px_add_entropy(const uint8 *data, unsigned count)
{
/*
* estimate 0 bits
*/
RAND_add(data, count, 0);
return 0;
}
示例10: openssl_random_add
/***
mixes the num bytes at buf into the PRNG state.
@function rand_add
@tparam string seed data to seed random generator
@tparam number entropy the lower bound of an estimate of how much randomness is contained in buf, measured in bytes.
*/
static int openssl_random_add(lua_State*L)
{
size_t num = 0;
const void *buf = luaL_checklstring(L, 1, &num);
double entropy = luaL_optinteger(L, 2, num);
RAND_add(buf, num, entropy);
return 0;
}
示例11: RAND_poll
/*
* the FAQ indicates we need to provide at least 20 bytes (160 bits) of
* seed
*/
int RAND_poll(void)
{
unsigned long l;
unsigned long tsc;
int i;
/*
* There are several options to gather miscellaneous data but for now we
* will loop checking the time stamp counter (rdtsc) and the
* SuperHighResolutionTimer. Each iteration will collect 8 bytes of data
* but it is treated as only 1 byte of entropy. The call to
* ThreadSwitchWithDelay() will introduce additional variability into the
* data returned by rdtsc. Applications can argument the seed material
* by adding additional stuff with RAND_add() and should probably do so.
*/
l = GetProcessSwitchCount();
RAND_add(&l, sizeof(l), 1);
/* need to cast the void* to unsigned long here */
l = (unsigned long)RunningProcess;
RAND_add(&l, sizeof(l), 1);
for (i = 2; i < ENTROPY_NEEDED; i++) {
# ifdef __MWERKS__
asm {
rdtsc mov tsc, eax}
# elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
asm volatile ("rdtsc":"=a" (tsc)::"edx");
# endif
RAND_add(&tsc, sizeof(tsc), 1);
l = GetSuperHighResolutionTimer();
RAND_add(&l, sizeof(l), 0);
# if defined(NETWARE_LIBC)
NXThreadYield();
# else /* NETWARE_CLIB */
ThreadSwitchWithDelay();
# endif
}
return 1;
}
示例12: RAND_poll
int RAND_poll(void)
{
long pid, iosb[2];
int status = 0;
struct {
short length, code;
long *buffer;
int *retlen;
} item[32], *pitem;
unsigned char data_buffer[256];
short total_length = 0;
struct items_data_st *pitems_data;
pitems_data = items_data;
pitem = item;
/* Setup */
while (pitems_data->length && (total_length + pitems_data->length <= 256)) {
pitem->length = pitems_data->length;
pitem->code = pitems_data->code;
pitem->buffer = (long *)&data_buffer[total_length];
pitem->retlen = 0;
total_length += pitems_data->length;
pitems_data++;
pitem ++;
}
pitem->length = pitem->code = 0;
/*
* Scan through all the processes in the system and add entropy with
* results from the processes that were possible to look at.
* However, view the information as only half trustable.
*/
pid = -1; /* search context */
while ((status = sys$getjpiw(0, &pid, 0, item, iosb, 0, 0))
!= SS$_NOMOREPROC) {
if (status == SS$_NORMAL) {
RAND_add((PTR_T) data_buffer, total_length, total_length / 2);
}
}
sys$gettim(iosb);
RAND_add((PTR_T) iosb, sizeof(iosb), sizeof(iosb) / 2);
return 1;
}
示例13: test_rand_add
static int test_rand_add(void)
{
char *p;
if (!TEST_ptr(p = malloc(RAND_ADD_SIZE)))
return 0;
RAND_add(p, RAND_ADD_SIZE, RAND_ADD_SIZE);
free(p);
return 1;
}
示例14: sqlcipher_openssl_add_random
static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
sqlite3_mutex_enter(openssl_rand_mutex);
#endif
RAND_add(buffer, length, 0);
#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
sqlite3_mutex_leave(openssl_rand_mutex);
#endif
return SQLITE_OK;
}
示例15: RAND_load_file
int RAND_load_file(const char *file, long bytes)
{
/* If bytes >= 0, read up to 'bytes' bytes.
* if bytes == -1, read complete file. */
MS_STATIC unsigned char buf[BUFSIZE];
struct stat sb;
int i,ret=0,n;
FILE *in;
if (file == NULL) return(0);
i=stat(file,&sb);
/* If the state fails, put some crap in anyway */
RAND_add(&sb,sizeof(sb),0);
if (i < 0) return(0);
if (bytes == 0) return(ret);
in=fopen(file,"rb");
if (in == NULL) goto err;
for (;;)
{
if (bytes > 0)
n = (bytes < BUFSIZE)?(int)bytes:BUFSIZE;
else
n = BUFSIZE;
i=fread(buf,1,n,in);
if (i <= 0) break;
/* even if n != i, use the full array */
RAND_add(buf,n,i);
ret+=i;
if (bytes > 0)
{
bytes-=n;
if (bytes == 0) break;
}
}
fclose(in);
memset(buf,0,BUFSIZE);
err:
return(ret);
}