-
Notifications
You must be signed in to change notification settings - Fork 1
/
mini_random_win.cpp
65 lines (47 loc) · 982 Bytes
/
mini_random_win.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "mini_random_win.h"
static HCRYPTPROV s_crypt_ctx = NULL;
MINI_NAMESPACE_BEGIN
bool init_rand_ctx()
{
return CryptAcquireContext(&s_crypt_ctx, nullptr, nullptr, PROV_RSA_FULL, 0)
? true : false;
}
int gen_win_random()
{
if (!s_crypt_ctx) {
return 0;
}
int random = 0;
CryptGenRandom(s_crypt_ctx, 4, (BYTE *)&random);
return random;
}
void unit_rand_ctx()
{
if (s_crypt_ctx) {
CryptReleaseContext(s_crypt_ctx, 0);
}
}
RandomWin::RandomWin()
: m_hCryptCtx(NULL)
, m_CurRandom(0)
{
if (!CryptAcquireContext(&m_hCryptCtx, nullptr, nullptr, PROV_RSA_FULL, 0)) {
m_hCryptCtx = NULL;
}
}
RandomWin::~RandomWin()
{
if (m_hCryptCtx) {
CryptReleaseContext(m_hCryptCtx, 0);
}
}
int RandomWin::getRandom()
{
if (!m_hCryptCtx) {
return 0;
}
m_CurRandom = 0;
CryptGenRandom(m_hCryptCtx, 4, (BYTE *)&m_CurRandom);
return m_CurRandom;
}
MINI_NAMESPACE_END