Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarSecureRandom_windows.cpp
blob: e118991bf41fd667bc7a26d4330725ea133cd6e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "StarSecureRandom.hpp"
#include <windows.h>
#include <wincrypt.h>

namespace Star {

ByteArray secureRandomBytes(size_t size) {
  HCRYPTPROV context = 0;
  auto res = ByteArray(size, '\0');

  CryptAcquireContext(&context, 0, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
  auto success = CryptGenRandom(context, size, (PBYTE)res.ptr());
  CryptReleaseContext(context, 0);

  if (!success)
    throw StarException("Could not read random bytes from source.");

  return res;
}

}