本文整理汇总了C++中CryptGenRandom函数的典型用法代码示例。如果您正苦于以下问题:C++ CryptGenRandom函数的具体用法?C++ CryptGenRandom怎么用?C++ CryptGenRandom使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CryptGenRandom函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: secureRand
//Return random bytes
bool secureRand(char **output, const int size)
{
//Allocate bytes
*output = (char *) malloc(sizeof(char) * size);
#ifdef _WIN32
HCRYPTPROV hCryptProv = 0;
//Prepare CSPRNG
if (!CryptAcquireContextW
(&hCryptProv, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
fprintf(stderr,
"Error: secureRand: CryptAcquireContextW failed");
return 0;
}
//Generate bytes
if (!CryptGenRandom(hCryptProv, size, (unsigned char *) *output)) {
fprintf(stderr,
"Error: secureRand: CryptGenRandom failed");
return 0;
}
#else
//FIXME: read from /dev/urandom
for (int i = 0; i < size; i++) {
(*output)[i] = rand() % 256 - 128;
}
#endif
return 1;
}
示例2: win32_urandom
/* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen
API. Return 0 on success, or raise an exception and return -1 on error. */
static int
win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
{
Py_ssize_t chunk;
if (hCryptProv == 0)
{
if (win32_urandom_init(raise) == -1)
return -1;
}
while (size > 0)
{
chunk = size > INT_MAX ? INT_MAX : size;
if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer))
{
/* CryptGenRandom() failed */
if (raise)
PyErr_SetFromWindowsErr(0);
else
Py_FatalError("Failed to initialized the randomized hash "
"secret using CryptoGen)");
return -1;
}
buffer += chunk;
size -= chunk;
}
return 0;
}
示例3: GenerateRandomBytes
static
nsresult
GenerateRandomBytes(uint32_t aSize,
uint8_t* _buffer)
{
// On Windows, we'll use its built-in cryptographic API.
#if defined(XP_WIN)
HCRYPTPROV cryptoProvider;
BOOL rc = CryptAcquireContext(&cryptoProvider, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
if (rc) {
rc = CryptGenRandom(cryptoProvider, aSize, _buffer);
(void)CryptReleaseContext(cryptoProvider, 0);
}
return rc ? NS_OK : NS_ERROR_FAILURE;
// On Unix, we'll just read in from /dev/urandom.
#elif defined(XP_UNIX)
NS_ENSURE_ARG_MAX(aSize, INT32_MAX);
PRFileDesc* urandom = PR_Open("/dev/urandom", PR_RDONLY, 0);
nsresult rv = NS_ERROR_FAILURE;
if (urandom) {
int32_t bytesRead = PR_Read(urandom, _buffer, aSize);
if (bytesRead == static_cast<int32_t>(aSize)) {
rv = NS_OK;
}
(void)PR_Close(urandom);
}
return rv;
#endif
}
示例4: make_random_string
int make_random_string(char* out) {
char buf[256];
#ifdef _WIN32
HCRYPTPROV hCryptProv;
if(! CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
return -1;
}
if(! CryptGenRandom(hCryptProv, (DWORD) 32, (BYTE *) buf)) {
CryptReleaseContext(hCryptProv, 0);
return -1;
}
CryptReleaseContext(hCryptProv, 0);
#else
FILE* f = fopen("/dev/random", "r");
if (!f) {
return -1;
}
size_t n = fread(buf, 32, 1, f);
fclose(f);
if (n != 1) return -1;
#endif
md5_block((const unsigned char*)buf, 32, out);
return 0;
}
示例5: rand_open
static rand_t *
rand_open(void)
{
rand_t *r;
u_char seed[256];
#if 0
#ifdef _WIN32
HCRYPTPROV hcrypt = 0;
CryptAcquireContext(&hcrypt, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT);
CryptGenRandom(hcrypt, sizeof(seed), seed);
CryptReleaseContext(hcrypt, 0);
#else
struct timeval *tv = (struct timeval *)seed;
int fd;
if ((fd = open("/dev/arandom", O_RDONLY)) != -1 ||
(fd = open("/dev/urandom", O_RDONLY)) != -1) {
read(fd, seed + sizeof(*tv), sizeof(seed) - sizeof(*tv));
close(fd);
}
gettimeofday(tv, NULL);
#endif
#endif
if ((r = (rand_t *)malloc(sizeof(*r))) != NULL) {
rand_init(r);
rand_addrandom(r, seed, 128);
rand_addrandom(r, seed + 128, 128);
r->tmp = NULL;
r->tmplen = 0;
}
return (r);
}
示例6: defined
void NonblockingRng::GenerateBlock(byte *output, size_t size)
{
#ifdef CRYPTOPP_WIN32_AVAILABLE
// Acquiring a provider is expensive. Do it once and retain the reference.
static const MicrosoftCryptoProvider &hProvider = Singleton<MicrosoftCryptoProvider>().Ref();
# if defined(USE_MS_CRYPTOAPI)
if (!CryptGenRandom(hProvider.GetProviderHandle(), (DWORD)size, output))
throw OS_RNG_Err("CryptGenRandom");
# elif defined(USE_MS_CNGAPI)
NTSTATUS ret = BCryptGenRandom(hProvider.GetProviderHandle(), output, (ULONG)size, 0);
if (!(BCRYPT_SUCCESS(ret)))
{
// Hack... OS_RNG_Err calls GetLastError()
SetLastError(NtStatusToErrorCode(ret));
throw OS_RNG_Err("BCryptGenRandom");
}
# endif
#else
while (size)
{
ssize_t len = read(m_fd, output, size);
if (len < 0)
{
// /dev/urandom reads CAN give EAGAIN errors! (maybe EINTR as well)
if (errno != EINTR && errno != EAGAIN)
throw OS_RNG_Err("read /dev/urandom");
continue;
}
output += len;
size -= len;
}
#endif // CRYPTOPP_WIN32_AVAILABLE
}
示例7: __declspec
// from tweetnacl
__declspec(dllexport) void randombytes(unsigned char *x,int xlen)
{
HCRYPTPROV prov = 0;
CryptAcquireContextW(&prov, NULL, NULL,PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
CryptGenRandom(prov, xlen, x);
CryptReleaseContext(prov, 0);
}
示例8: I_MakeRNGSeed
unsigned int I_MakeRNGSeed()
{
unsigned int seed;
// If RtlGenRandom is available, use that to avoid increasing the
// working set by pulling in all of the crytographic API.
HMODULE advapi = GetModuleHandle("advapi32.dll");
if (advapi != NULL)
{
BOOLEAN (APIENTRY *RtlGenRandom)(void *, ULONG) =
(BOOLEAN (APIENTRY *)(void *, ULONG))GetProcAddress(advapi, "SystemFunction036");
if (RtlGenRandom != NULL)
{
if (RtlGenRandom(&seed, sizeof(seed)))
{
return seed;
}
}
}
// Use the full crytographic API to produce a seed. If that fails,
// time() is used as a fallback.
HCRYPTPROV prov;
if (!CryptAcquireContext(&prov, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
return (unsigned int)time(NULL);
}
if (!CryptGenRandom(prov, sizeof(seed), (BYTE *)&seed))
{
seed = (unsigned int)time(NULL);
}
CryptReleaseContext(prov, 0);
return seed;
}
示例9: get_random_bytes
static int TSK_WARN_UNUSED
get_random_bytes(uint8_t *buf)
{
/* Based on CPython's code in bootstrap_hash.c */
int ret = TSK_ERR_GENERATE_UUID;
HCRYPTPROV hCryptProv = NULL;
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
goto out;
}
if (!CryptGenRandom(hCryptProv, (DWORD) UUID_NUM_BYTES, buf)) {
goto out;
}
if (!CryptReleaseContext(hCryptProv, 0)) {
hCryptProv = NULL;
goto out;
}
hCryptProv = NULL;
ret = 0;
out:
if (hCryptProv != NULL) {
CryptReleaseContext(hCryptProv, 0);
}
return ret;
}
示例10: test_gen_random
static void test_gen_random(void)
{
BOOL result;
BYTE rnd1[16], rnd2[16];
memset(rnd1, 0, sizeof(rnd1));
memset(rnd2, 0, sizeof(rnd2));
result = CryptGenRandom(hProv, sizeof(rnd1), rnd1);
ok(result, "%08x\n", (unsigned int)GetLastError());
result = CryptGenRandom(hProv, sizeof(rnd2), rnd2);
ok(result, "%08x\n", (unsigned int)GetLastError());
ok(memcmp(rnd1, rnd2, sizeof(rnd1)), "CryptGenRandom generates non random data\n");
}
示例11: w32crypto_bytes
static int
w32crypto_bytes(unsigned char *outdata, int size)
{
if (CryptGenRandom(_hc_CryptProvider(), size, outdata))
return 1;
return 0;
}
示例12: randomize
static Gc_rc
randomize (int level, char *data, size_t datalen)
{
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
if (!g_hProv)
return GC_RANDOM_ERROR;
CryptGenRandom (g_hProv, (DWORD) datalen, data);
#else
int fd;
const char *device;
size_t len = 0;
int rc;
switch (level)
{
case 0:
device = NAME_OF_NONCE_DEVICE;
break;
case 1:
device = NAME_OF_PSEUDO_RANDOM_DEVICE;
break;
default:
device = NAME_OF_RANDOM_DEVICE;
break;
}
if (strcmp (device, "no") == 0)
return GC_RANDOM_ERROR;
fd = open (device, O_RDONLY);
if (fd < 0)
return GC_RANDOM_ERROR;
do
{
ssize_t tmp;
tmp = read (fd, data, datalen);
if (tmp < 0)
{
int save_errno = errno;
close (fd);
errno = save_errno;
return GC_RANDOM_ERROR;
}
len += tmp;
}
while (len < datalen);
rc = close (fd);
if (rc < 0)
return GC_RANDOM_ERROR;
#endif
return GC_OK;
}
示例13: php_win32_get_random_bytes
PHPAPI int php_win32_get_random_bytes(unsigned char *buf, size_t size) { /* {{{ */
HCRYPTPROV hCryptProv;
int has_context = 0;
BOOL ret;
size_t i = 0;
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
/* Could mean that the key container does not exist, let try
again by asking for a new one */
if (GetLastError() == NTE_BAD_KEYSET) {
if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
has_context = 1;
} else {
return FAILURE;
}
}
}
ret = CryptGenRandom(hCryptProv, size, buf);
CryptReleaseContext(hCryptProv, 0);
if (ret) {
return SUCCESS;
}
return FAILURE;
}
示例14: GetOSRand
/** Get 32 bytes of system entropy. */
static void GetOSRand(unsigned char *ent32)
{
#ifdef WIN32
HCRYPTPROV hProvider;
int ret = CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
if (!ret) {
RandFailure();
}
ret = CryptGenRandom(hProvider, 32, ent32);
if (!ret) {
RandFailure();
}
CryptReleaseContext(hProvider, 0);
#else
int f = open("/dev/urandom", O_RDONLY);
if (f == -1) {
RandFailure();
}
int have = 0;
do {
ssize_t n = read(f, ent32 + have, 32 - have);
if (n <= 0 || n + have > 32) {
RandFailure();
}
have += n;
} while (have < 32);
close(f);
#endif
}
示例15: AJ_RandBytes
void AJ_RandBytes(uint8_t* rand, uint32_t len)
{
HCRYPTPROV hProvider;
CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
CryptGenRandom(hProvider, len, rand);
CryptReleaseContext(hProvider, 0);
}