ホーム › Security.Authentication.Identity › RtlGenRandom
RtlGenRandom
関数暗号品質の擬似乱数をバッファに生成する。
シグネチャ
// ADVAPI32.dll
#include <windows.h>
BOOLEAN RtlGenRandom(
void* RandomBuffer,
DWORD RandomBufferLength
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| RandomBuffer | void* | out | 生成された乱数バイト列を受け取る出力バッファ。 |
| RandomBufferLength | DWORD | in | 生成する乱数のバイト数。 |
戻り値の型: BOOLEAN
各言語での呼び出し定義
// ADVAPI32.dll
#include <windows.h>
BOOLEAN RtlGenRandom(
void* RandomBuffer,
DWORD RandomBufferLength
);[return: MarshalAs(UnmanagedType.U1)]
[DllImport("ADVAPI32.dll", ExactSpelling = true)]
static extern bool RtlGenRandom(
IntPtr RandomBuffer, // void* out
uint RandomBufferLength // DWORD
);<DllImport("ADVAPI32.dll", ExactSpelling:=True)>
Public Shared Function RtlGenRandom(
RandomBuffer As IntPtr, ' void* out
RandomBufferLength As UInteger ' DWORD
) As <MarshalAs(UnmanagedType.U1)> Boolean
End Function' RandomBuffer : void* out
' RandomBufferLength : DWORD
Declare PtrSafe Function RtlGenRandom Lib "advapi32" Alias "SystemFunction036" ( _
ByVal RandomBuffer As LongPtr, _
ByVal RandomBufferLength As Long) As Byte
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
RtlGenRandom = ctypes.windll.advapi32.RtlGenRandom
RtlGenRandom.restype = wintypes.BOOLEAN
RtlGenRandom.argtypes = [
ctypes.POINTER(None), # RandomBuffer : void* out
wintypes.DWORD, # RandomBufferLength : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
RtlGenRandom = Fiddle::Function.new(
lib['SystemFunction036'],
[
Fiddle::TYPE_VOIDP, # RandomBuffer : void* out
-Fiddle::TYPE_INT, # RandomBufferLength : DWORD
],
Fiddle::TYPE_CHAR)#[link(name = "advapi32")]
extern "system" {
fn RtlGenRandom(
RandomBuffer: *mut (), // void* out
RandomBufferLength: u32 // DWORD
) -> u8;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.U1)]
[DllImport("ADVAPI32.dll")]
public static extern bool RtlGenRandom(IntPtr RandomBuffer, uint RandomBufferLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RtlGenRandom' -Namespace Win32 -PassThru
# $api::RtlGenRandom(RandomBuffer, RandomBufferLength)#uselib "ADVAPI32.dll"
#func global RtlGenRandom "SystemFunction036" sptr, sptr
; RtlGenRandom RandomBuffer, RandomBufferLength ; 戻り値は stat
; RandomBuffer : void* out -> "sptr"
; RandomBufferLength : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll"
#cfunc global RtlGenRandom "SystemFunction036" sptr, int
; res = RtlGenRandom(RandomBuffer, RandomBufferLength)
; RandomBuffer : void* out -> "sptr"
; RandomBufferLength : DWORD -> "int"; BOOLEAN RtlGenRandom(void* RandomBuffer, DWORD RandomBufferLength)
#uselib "ADVAPI32.dll"
#cfunc global RtlGenRandom "SystemFunction036" intptr, int
; res = RtlGenRandom(RandomBuffer, RandomBufferLength)
; RandomBuffer : void* out -> "intptr"
; RandomBufferLength : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procRtlGenRandom = advapi32.NewProc("SystemFunction036")
)
// RandomBuffer (void* out), RandomBufferLength (DWORD)
r1, _, err := procRtlGenRandom.Call(
uintptr(RandomBuffer),
uintptr(RandomBufferLength),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLEANfunction RtlGenRandom(
RandomBuffer: Pointer; // void* out
RandomBufferLength: DWORD // DWORD
): ByteBool; stdcall;
external 'ADVAPI32.dll' name 'SystemFunction036';result := DllCall("ADVAPI32\SystemFunction036"
, "Ptr", RandomBuffer ; void* out
, "UInt", RandomBufferLength ; DWORD
, "Char") ; return: BOOLEAN●RtlGenRandom(RandomBuffer, RandomBufferLength) = DLL("ADVAPI32.dll", "byte SystemFunction036(void*, dword)")
# 呼び出し: RtlGenRandom(RandomBuffer, RandomBufferLength)
# RandomBuffer : void* out -> "void*"
# RandomBufferLength : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "advapi32" fn RtlGenRandom(
RandomBuffer: ?*anyopaque, // void* out
RandomBufferLength: u32 // DWORD
) callconv(std.os.windows.WINAPI) u8;proc RtlGenRandom(
RandomBuffer: pointer, # void* out
RandomBufferLength: uint32 # DWORD
): uint8 {.importc: "SystemFunction036", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
ubyte RtlGenRandom(
void* RandomBuffer, // void* out
uint RandomBufferLength // DWORD
);ccall((:SystemFunction036, "ADVAPI32.dll"), stdcall, UInt8,
(Ptr{Cvoid}, UInt32),
RandomBuffer, RandomBufferLength)
# RandomBuffer : void* out -> Ptr{Cvoid}
# RandomBufferLength : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint8_t SystemFunction036(
void* RandomBuffer,
uint32_t RandomBufferLength);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.SystemFunction036(RandomBuffer, RandomBufferLength)
-- RandomBuffer : void* out
-- RandomBufferLength : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const RtlGenRandom = lib.func('__stdcall', 'SystemFunction036', 'uint8_t', ['void *', 'uint32_t']);
// RtlGenRandom(RandomBuffer, RandomBufferLength)
// RandomBuffer : void* out -> 'void *'
// RandomBufferLength : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
SystemFunction036: { parameters: ["pointer", "u32"], result: "u8" },
});
// lib.symbols.SystemFunction036(RandomBuffer, RandomBufferLength)
// RandomBuffer : void* out -> "pointer"
// RandomBufferLength : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint8_t SystemFunction036(
void* RandomBuffer,
uint32_t RandomBufferLength);
C, "ADVAPI32.dll");
// $ffi->SystemFunction036(RandomBuffer, RandomBufferLength);
// RandomBuffer : void* out
// RandomBufferLength : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Advapi32 extends StdCallLibrary {
Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class);
byte RtlGenRandom(
Pointer RandomBuffer, // void* out
int RandomBufferLength // DWORD
);
}@[Link("advapi32")]
lib LibADVAPI32
fun RtlGenRandom = SystemFunction036(
RandomBuffer : Void*, # void* out
RandomBufferLength : UInt32 # DWORD
) : UInt8
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef RtlGenRandomNative = Uint8 Function(Pointer<Void>, Uint32);
typedef RtlGenRandomDart = int Function(Pointer<Void>, int);
final RtlGenRandom = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<RtlGenRandomNative, RtlGenRandomDart>('SystemFunction036');
// RandomBuffer : void* out -> Pointer<Void>
// RandomBufferLength : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RtlGenRandom(
RandomBuffer: Pointer; // void* out
RandomBufferLength: DWORD // DWORD
): ByteBool; stdcall;
external 'ADVAPI32.dll' name 'SystemFunction036';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SystemFunction036"
c_RtlGenRandom :: Ptr () -> Word32 -> IO Word8
-- RandomBuffer : void* out -> Ptr ()
-- RandomBufferLength : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let rtlgenrandom =
foreign "SystemFunction036"
((ptr void) @-> uint32_t @-> returning uint8_t)
(* RandomBuffer : void* out -> (ptr void) *)
(* RandomBufferLength : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("SystemFunction036" rtl-gen-random :convention :stdcall) :uint8
(random-buffer :pointer) ; void* out
(random-buffer-length :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RtlGenRandom = Win32::API::More->new('ADVAPI32',
'BYTE SystemFunction036(LPVOID RandomBuffer, DWORD RandomBufferLength)');
# my $ret = $RtlGenRandom->Call($RandomBuffer, $RandomBufferLength);
# RandomBuffer : void* out -> LPVOID
# RandomBufferLength : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。