ホーム › Security.Cryptography › CryptGenRandom
CryptGenRandom
関数暗号学的に安全な乱数バイト列をバッファに生成する。
シグネチャ
// ADVAPI32.dll
#include <windows.h>
BOOL CryptGenRandom(
UINT_PTR hProv,
DWORD dwLen,
BYTE* pbBuffer
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| hProv | UINT_PTR | in |
| dwLen | DWORD | in |
| pbBuffer | BYTE* | inout |
戻り値の型: BOOL
各言語での呼び出し定義
// ADVAPI32.dll
#include <windows.h>
BOOL CryptGenRandom(
UINT_PTR hProv,
DWORD dwLen,
BYTE* pbBuffer
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptGenRandom(
UIntPtr hProv, // UINT_PTR
uint dwLen, // DWORD
IntPtr pbBuffer // BYTE* in/out
);<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptGenRandom(
hProv As UIntPtr, ' UINT_PTR
dwLen As UInteger, ' DWORD
pbBuffer As IntPtr ' BYTE* in/out
) As Boolean
End Function' hProv : UINT_PTR
' dwLen : DWORD
' pbBuffer : BYTE* in/out
Declare PtrSafe Function CryptGenRandom Lib "advapi32" ( _
ByVal hProv As LongPtr, _
ByVal dwLen As Long, _
ByVal pbBuffer As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CryptGenRandom = ctypes.windll.advapi32.CryptGenRandom
CryptGenRandom.restype = wintypes.BOOL
CryptGenRandom.argtypes = [
ctypes.c_size_t, # hProv : UINT_PTR
wintypes.DWORD, # dwLen : DWORD
ctypes.POINTER(ctypes.c_ubyte), # pbBuffer : BYTE* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
CryptGenRandom = Fiddle::Function.new(
lib['CryptGenRandom'],
[
Fiddle::TYPE_UINTPTR_T, # hProv : UINT_PTR
-Fiddle::TYPE_INT, # dwLen : DWORD
Fiddle::TYPE_VOIDP, # pbBuffer : BYTE* in/out
],
Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn CryptGenRandom(
hProv: usize, // UINT_PTR
dwLen: u32, // DWORD
pbBuffer: *mut u8 // BYTE* in/out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool CryptGenRandom(UIntPtr hProv, uint dwLen, IntPtr pbBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptGenRandom' -Namespace Win32 -PassThru
# $api::CryptGenRandom(hProv, dwLen, pbBuffer)#uselib "ADVAPI32.dll"
#func global CryptGenRandom "CryptGenRandom" sptr, sptr, sptr
; CryptGenRandom hProv, dwLen, varptr(pbBuffer) ; 戻り値は stat
; hProv : UINT_PTR -> "sptr"
; dwLen : DWORD -> "sptr"
; pbBuffer : BYTE* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "ADVAPI32.dll" #cfunc global CryptGenRandom "CryptGenRandom" sptr, int, var ; res = CryptGenRandom(hProv, dwLen, pbBuffer) ; hProv : UINT_PTR -> "sptr" ; dwLen : DWORD -> "int" ; pbBuffer : BYTE* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global CryptGenRandom "CryptGenRandom" sptr, int, sptr ; res = CryptGenRandom(hProv, dwLen, varptr(pbBuffer)) ; hProv : UINT_PTR -> "sptr" ; dwLen : DWORD -> "int" ; pbBuffer : BYTE* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL CryptGenRandom(UINT_PTR hProv, DWORD dwLen, BYTE* pbBuffer) #uselib "ADVAPI32.dll" #cfunc global CryptGenRandom "CryptGenRandom" intptr, int, var ; res = CryptGenRandom(hProv, dwLen, pbBuffer) ; hProv : UINT_PTR -> "intptr" ; dwLen : DWORD -> "int" ; pbBuffer : BYTE* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOL CryptGenRandom(UINT_PTR hProv, DWORD dwLen, BYTE* pbBuffer) #uselib "ADVAPI32.dll" #cfunc global CryptGenRandom "CryptGenRandom" intptr, int, intptr ; res = CryptGenRandom(hProv, dwLen, varptr(pbBuffer)) ; hProv : UINT_PTR -> "intptr" ; dwLen : DWORD -> "int" ; pbBuffer : BYTE* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procCryptGenRandom = advapi32.NewProc("CryptGenRandom")
)
// hProv (UINT_PTR), dwLen (DWORD), pbBuffer (BYTE* in/out)
r1, _, err := procCryptGenRandom.Call(
uintptr(hProv),
uintptr(dwLen),
uintptr(pbBuffer),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction CryptGenRandom(
hProv: NativeUInt; // UINT_PTR
dwLen: DWORD; // DWORD
pbBuffer: Pointer // BYTE* in/out
): BOOL; stdcall;
external 'ADVAPI32.dll' name 'CryptGenRandom';result := DllCall("ADVAPI32\CryptGenRandom"
, "UPtr", hProv ; UINT_PTR
, "UInt", dwLen ; DWORD
, "Ptr", pbBuffer ; BYTE* in/out
, "Int") ; return: BOOL●CryptGenRandom(hProv, dwLen, pbBuffer) = DLL("ADVAPI32.dll", "bool CryptGenRandom(int, dword, void*)")
# 呼び出し: CryptGenRandom(hProv, dwLen, pbBuffer)
# hProv : UINT_PTR -> "int"
# dwLen : DWORD -> "dword"
# pbBuffer : BYTE* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。