Generate a random secret key by using the Win32 API. Raises LoadError if the current platform cannot make use of the Win32 API. Raises SystemCallError if some other error occured.
Source Code
# File rails_generator/secret_key_generator.rb, line 28 def generate_secret_with_win32_api # Following code is based on David Garamond's GUID library for Ruby. require 'Win32API' crypt_acquire_context = Win32API.new("advapi32", "CryptAcquireContext", 'PPPII', 'L') crypt_gen_random = Win32API.new("advapi32", "CryptGenRandom", 'LIP', 'L') crypt_release_context = Win32API.new("advapi32", "CryptReleaseContext", 'LI', 'L') prov_rsa_full = 1 crypt_verifycontext = 0xF0000000 hProvStr = " " * 4 if crypt_acquire_context.call(hProvStr, nil, nil, prov_rsa_full, crypt_verifycontext) == 0 raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}" end hProv, = hProvStr.unpack('L') bytes = " " * 64 if crypt_gen_random.call(hProv, bytes.size, bytes) == 0 raise SystemCallError, "CryptGenRandom failed: #{lastWin32ErrorMessage}" end if crypt_release_context.call(hProv, 0) == 0 raise SystemCallError, "CryptReleaseContext failed: #{lastWin32ErrorMessage}" end bytes.unpack("H*")[0] end
<code/>and<pre/>for code samples.