Module: SecureRandom::AdvApi32

Extended by:
Fiddle::Importer
Defined in:
lib/securerandom.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.gen_random(n) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/securerandom.rb', line 65

def self.gen_random(n)
  @hProv ||= get_provider
  bytes = " ".force_encoding("ASCII-8BIT") * n
  if CryptGenRandom(@hProv, bytes.size, bytes) == 0
    raise SystemCallError, "CryptGenRandom failed: #{Kernel32.last_error_message}"
  end
  bytes
end

.get_providerObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/securerandom.rb', line 52

def self.get_provider
  hProvStr = " " * Fiddle::SIZEOF_VOIDP
  prov_rsa_full = 1
  crypt_verifycontext = 0xF0000000

  if CryptAcquireContext(hProvStr, nil, nil, prov_rsa_full, crypt_verifycontext) == 0
    raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}"
  end
  type = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q' : 'l'
  hProv, = hProvStr.unpack(type)
  hProv
end