|
|
@ -7,7 +7,9 @@ |
|
|
|
http://opensource.org/licenses/BSD-3-Clause
|
|
|
|
http://opensource.org/licenses/BSD-3-Clause
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef CYBOZU_DONT_USE_EXCEPTION |
|
|
|
#include <cybozu/exception.hpp> |
|
|
|
#include <cybozu/exception.hpp> |
|
|
|
|
|
|
|
#endif |
|
|
|
#ifdef _WIN32 |
|
|
|
#ifdef _WIN32 |
|
|
|
#include <winsock2.h> |
|
|
|
#include <winsock2.h> |
|
|
|
#include <windows.h> |
|
|
|
#include <windows.h> |
|
|
@ -27,22 +29,6 @@ class RandomGenerator { |
|
|
|
RandomGenerator(const RandomGenerator&); |
|
|
|
RandomGenerator(const RandomGenerator&); |
|
|
|
void operator=(const RandomGenerator&); |
|
|
|
void operator=(const RandomGenerator&); |
|
|
|
public: |
|
|
|
public: |
|
|
|
uint32_t operator()() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return get32(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
uint32_t get32() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint32_t ret; |
|
|
|
|
|
|
|
read(&ret, 1); |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
uint64_t get64() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint64_t ret; |
|
|
|
|
|
|
|
read(&ret, 1); |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
#ifdef _WIN32 |
|
|
|
#ifdef _WIN32 |
|
|
|
RandomGenerator() |
|
|
|
RandomGenerator() |
|
|
|
: prov_(0) |
|
|
|
: prov_(0) |
|
|
@ -52,10 +38,15 @@ public: |
|
|
|
for (int i = 0; i < 2; i++) { |
|
|
|
for (int i = 0; i < 2; i++) { |
|
|
|
if (CryptAcquireContext(&prov_, NULL, NULL, PROV_RSA_FULL, flagTbl[i]) != 0) return; |
|
|
|
if (CryptAcquireContext(&prov_, NULL, NULL, PROV_RSA_FULL, flagTbl[i]) != 0) return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#ifdef CYBOZU_DONT_USE_EXCEPTION |
|
|
|
|
|
|
|
prov_ = 0; |
|
|
|
|
|
|
|
#else |
|
|
|
throw cybozu::Exception("randomgenerator"); |
|
|
|
throw cybozu::Exception("randomgenerator"); |
|
|
|
|
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
bool read_inner(void *buf, size_t byteSize) |
|
|
|
bool read_inner(void *buf, size_t byteSize) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
if (prov_ == 0) return false; |
|
|
|
return CryptGenRandom(prov_, static_cast<DWORD>(byteSize), static_cast<BYTE*>(buf)) != 0; |
|
|
|
return CryptGenRandom(prov_, static_cast<DWORD>(byteSize), static_cast<BYTE*>(buf)) != 0; |
|
|
|
} |
|
|
|
} |
|
|
|
~RandomGenerator() |
|
|
|
~RandomGenerator() |
|
|
@ -88,13 +79,6 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
*pb = true; |
|
|
|
*pb = true; |
|
|
|
} |
|
|
|
} |
|
|
|
template<class T> |
|
|
|
|
|
|
|
void read(T *buf, size_t bufNum) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
bool b; |
|
|
|
|
|
|
|
read(&b, buf, bufNum); |
|
|
|
|
|
|
|
if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
HCRYPTPROV prov_; |
|
|
|
HCRYPTPROV prov_; |
|
|
|
static const size_t bufSize = 1024; |
|
|
|
static const size_t bufSize = 1024; |
|
|
@ -105,7 +89,9 @@ private: |
|
|
|
RandomGenerator() |
|
|
|
RandomGenerator() |
|
|
|
: fp_(::fopen("/dev/urandom", "rb")) |
|
|
|
: fp_(::fopen("/dev/urandom", "rb")) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
#ifndef CYBOZU_DONT_USE_EXCEPTION |
|
|
|
if (!fp_) throw cybozu::Exception("randomgenerator"); |
|
|
|
if (!fp_) throw cybozu::Exception("randomgenerator"); |
|
|
|
|
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
~RandomGenerator() |
|
|
|
~RandomGenerator() |
|
|
|
{ |
|
|
|
{ |
|
|
@ -121,6 +107,8 @@ private: |
|
|
|
const size_t byteSize = sizeof(T) * bufNum; |
|
|
|
const size_t byteSize = sizeof(T) * bufNum; |
|
|
|
*pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize; |
|
|
|
*pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
#ifndef CYBOZU_DONT_USE_EXCEPTION |
|
|
|
template<class T> |
|
|
|
template<class T> |
|
|
|
void read(T *buf, size_t bufNum) |
|
|
|
void read(T *buf, size_t bufNum) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -128,6 +116,22 @@ private: |
|
|
|
read(&b, buf, bufNum); |
|
|
|
read(&b, buf, bufNum); |
|
|
|
if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum; |
|
|
|
if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
uint32_t get32() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint32_t ret; |
|
|
|
|
|
|
|
read(&ret, 1); |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
uint64_t get64() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
uint64_t ret; |
|
|
|
|
|
|
|
read(&ret, 1); |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
uint32_t operator()() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return get32(); |
|
|
|
|
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
private: |
|
|
|
private: |
|
|
|
FILE *fp_; |
|
|
|
FILE *fp_; |
|
|
|