Win32 API 日本語リファレンス
ホームSecurity.Cryptography › CryptSignHashW

CryptSignHashW

関数
ハッシュ値に秘密鍵でデジタル署名を行う(Unicode版)。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

BOOL CryptSignHashW(
    UINT_PTR hHash,
    DWORD dwKeySpec,
    LPCWSTR szDescription,   // optional
    DWORD dwFlags,
    BYTE* pbSignature,   // optional
    DWORD* pdwSigLen
);

パラメーター

名前方向
hHashUINT_PTRin
dwKeySpecDWORDin
szDescriptionLPCWSTRinoptional
dwFlagsDWORDin
pbSignatureBYTE*outoptional
pdwSigLenDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

BOOL CryptSignHashW(
    UINT_PTR hHash,
    DWORD dwKeySpec,
    LPCWSTR szDescription,   // optional
    DWORD dwFlags,
    BYTE* pbSignature,   // optional
    DWORD* pdwSigLen
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool CryptSignHashW(
    UIntPtr hHash,   // UINT_PTR
    uint dwKeySpec,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] string szDescription,   // LPCWSTR optional
    uint dwFlags,   // DWORD
    IntPtr pbSignature,   // BYTE* optional, out
    ref uint pdwSigLen   // DWORD* in/out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptSignHashW(
    hHash As UIntPtr,   ' UINT_PTR
    dwKeySpec As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> szDescription As String,   ' LPCWSTR optional
    dwFlags As UInteger,   ' DWORD
    pbSignature As IntPtr,   ' BYTE* optional, out
    ByRef pdwSigLen As UInteger   ' DWORD* in/out
) As Boolean
End Function
' hHash : UINT_PTR
' dwKeySpec : DWORD
' szDescription : LPCWSTR optional
' dwFlags : DWORD
' pbSignature : BYTE* optional, out
' pdwSigLen : DWORD* in/out
Declare PtrSafe Function CryptSignHashW Lib "advapi32" ( _
    ByVal hHash As LongPtr, _
    ByVal dwKeySpec As Long, _
    ByVal szDescription As LongPtr, _
    ByVal dwFlags As Long, _
    ByVal pbSignature As LongPtr, _
    ByRef pdwSigLen As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptSignHashW = ctypes.windll.advapi32.CryptSignHashW
CryptSignHashW.restype = wintypes.BOOL
CryptSignHashW.argtypes = [
    ctypes.c_size_t,  # hHash : UINT_PTR
    wintypes.DWORD,  # dwKeySpec : DWORD
    wintypes.LPCWSTR,  # szDescription : LPCWSTR optional
    wintypes.DWORD,  # dwFlags : DWORD
    ctypes.POINTER(ctypes.c_ubyte),  # pbSignature : BYTE* optional, out
    ctypes.POINTER(wintypes.DWORD),  # pdwSigLen : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CryptSignHashW = Fiddle::Function.new(
  lib['CryptSignHashW'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hHash : UINT_PTR
    -Fiddle::TYPE_INT,  # dwKeySpec : DWORD
    Fiddle::TYPE_VOIDP,  # szDescription : LPCWSTR optional
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_VOIDP,  # pbSignature : BYTE* optional, out
    Fiddle::TYPE_VOIDP,  # pdwSigLen : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn CryptSignHashW(
        hHash: usize,  // UINT_PTR
        dwKeySpec: u32,  // DWORD
        szDescription: *const u16,  // LPCWSTR optional
        dwFlags: u32,  // DWORD
        pbSignature: *mut u8,  // BYTE* optional, out
        pdwSigLen: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool CryptSignHashW(UIntPtr hHash, uint dwKeySpec, [MarshalAs(UnmanagedType.LPWStr)] string szDescription, uint dwFlags, IntPtr pbSignature, ref uint pdwSigLen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptSignHashW' -Namespace Win32 -PassThru
# $api::CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
#uselib "ADVAPI32.dll"
#func global CryptSignHashW "CryptSignHashW" wptr, wptr, wptr, wptr, wptr, wptr
; CryptSignHashW hHash, dwKeySpec, szDescription, dwFlags, varptr(pbSignature), varptr(pdwSigLen)   ; 戻り値は stat
; hHash : UINT_PTR -> "wptr"
; dwKeySpec : DWORD -> "wptr"
; szDescription : LPCWSTR optional -> "wptr"
; dwFlags : DWORD -> "wptr"
; pbSignature : BYTE* optional, out -> "wptr"
; pdwSigLen : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CryptSignHashW "CryptSignHashW" sptr, int, wstr, int, var, var
; res = CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
; hHash : UINT_PTR -> "sptr"
; dwKeySpec : DWORD -> "int"
; szDescription : LPCWSTR optional -> "wstr"
; dwFlags : DWORD -> "int"
; pbSignature : BYTE* optional, out -> "var"
; pdwSigLen : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptSignHashW(UINT_PTR hHash, DWORD dwKeySpec, LPCWSTR szDescription, DWORD dwFlags, BYTE* pbSignature, DWORD* pdwSigLen)
#uselib "ADVAPI32.dll"
#cfunc global CryptSignHashW "CryptSignHashW" intptr, int, wstr, int, var, var
; res = CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
; hHash : UINT_PTR -> "intptr"
; dwKeySpec : DWORD -> "int"
; szDescription : LPCWSTR optional -> "wstr"
; dwFlags : DWORD -> "int"
; pbSignature : BYTE* optional, out -> "var"
; pdwSigLen : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCryptSignHashW = advapi32.NewProc("CryptSignHashW")
)

// hHash (UINT_PTR), dwKeySpec (DWORD), szDescription (LPCWSTR optional), dwFlags (DWORD), pbSignature (BYTE* optional, out), pdwSigLen (DWORD* in/out)
r1, _, err := procCryptSignHashW.Call(
	uintptr(hHash),
	uintptr(dwKeySpec),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(szDescription))),
	uintptr(dwFlags),
	uintptr(pbSignature),
	uintptr(pdwSigLen),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptSignHashW(
  hHash: NativeUInt;   // UINT_PTR
  dwKeySpec: DWORD;   // DWORD
  szDescription: PWideChar;   // LPCWSTR optional
  dwFlags: DWORD;   // DWORD
  pbSignature: Pointer;   // BYTE* optional, out
  pdwSigLen: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CryptSignHashW';
result := DllCall("ADVAPI32\CryptSignHashW"
    , "UPtr", hHash   ; UINT_PTR
    , "UInt", dwKeySpec   ; DWORD
    , "WStr", szDescription   ; LPCWSTR optional
    , "UInt", dwFlags   ; DWORD
    , "Ptr", pbSignature   ; BYTE* optional, out
    , "Ptr", pdwSigLen   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen) = DLL("ADVAPI32.dll", "bool CryptSignHashW(int, dword, char*, dword, void*, void*)")
# 呼び出し: CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
# hHash : UINT_PTR -> "int"
# dwKeySpec : DWORD -> "dword"
# szDescription : LPCWSTR optional -> "char*"
# dwFlags : DWORD -> "dword"
# pbSignature : BYTE* optional, out -> "void*"
# pdwSigLen : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。