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

CryptSignHashA

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

シグネチャ

// ADVAPI32.dll  (ANSI / -A)
#include <windows.h>

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

パラメーター

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

戻り値の型: BOOL

各言語での呼び出し定義

// ADVAPI32.dll  (ANSI / -A)
#include <windows.h>

BOOL CryptSignHashA(
    UINT_PTR hHash,
    DWORD dwKeySpec,
    LPCSTR szDescription,   // optional
    DWORD dwFlags,
    BYTE* pbSignature,   // optional
    DWORD* pdwSigLen
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool CryptSignHashA(
    UIntPtr hHash,   // UINT_PTR
    uint dwKeySpec,   // DWORD
    [MarshalAs(UnmanagedType.LPStr)] string szDescription,   // LPCSTR optional
    uint dwFlags,   // DWORD
    IntPtr pbSignature,   // BYTE* optional, out
    ref uint pdwSigLen   // DWORD* in/out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptSignHashA(
    hHash As UIntPtr,   ' UINT_PTR
    dwKeySpec As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPStr)> szDescription As String,   ' LPCSTR 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 : LPCSTR optional
' dwFlags : DWORD
' pbSignature : BYTE* optional, out
' pdwSigLen : DWORD* in/out
Declare PtrSafe Function CryptSignHashA Lib "advapi32" ( _
    ByVal hHash As LongPtr, _
    ByVal dwKeySpec As Long, _
    ByVal szDescription As String, _
    ByVal dwFlags As Long, _
    ByVal pbSignature As LongPtr, _
    ByRef pdwSigLen As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptSignHashA = ctypes.windll.advapi32.CryptSignHashA
CryptSignHashA.restype = wintypes.BOOL
CryptSignHashA.argtypes = [
    ctypes.c_size_t,  # hHash : UINT_PTR
    wintypes.DWORD,  # dwKeySpec : DWORD
    wintypes.LPCSTR,  # szDescription : LPCSTR 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')
CryptSignHashA = Fiddle::Function.new(
  lib['CryptSignHashA'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hHash : UINT_PTR
    -Fiddle::TYPE_INT,  # dwKeySpec : DWORD
    Fiddle::TYPE_VOIDP,  # szDescription : LPCSTR optional
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_VOIDP,  # pbSignature : BYTE* optional, out
    Fiddle::TYPE_VOIDP,  # pdwSigLen : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CryptSignHashA(
        hHash: usize,  // UINT_PTR
        dwKeySpec: u32,  // DWORD
        szDescription: *const u8,  // LPCSTR 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.Ansi, SetLastError = true)]
public static extern bool CryptSignHashA(UIntPtr hHash, uint dwKeySpec, [MarshalAs(UnmanagedType.LPStr)] string szDescription, uint dwFlags, IntPtr pbSignature, ref uint pdwSigLen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CryptSignHashA' -Namespace Win32 -PassThru
# $api::CryptSignHashA(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
#uselib "ADVAPI32.dll"
#func global CryptSignHashA "CryptSignHashA" sptr, sptr, sptr, sptr, sptr, sptr
; CryptSignHashA hHash, dwKeySpec, szDescription, dwFlags, varptr(pbSignature), varptr(pdwSigLen)   ; 戻り値は stat
; hHash : UINT_PTR -> "sptr"
; dwKeySpec : DWORD -> "sptr"
; szDescription : LPCSTR optional -> "sptr"
; dwFlags : DWORD -> "sptr"
; pbSignature : BYTE* optional, out -> "sptr"
; pdwSigLen : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CryptSignHashA "CryptSignHashA" sptr, int, str, int, var, var
; res = CryptSignHashA(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
; hHash : UINT_PTR -> "sptr"
; dwKeySpec : DWORD -> "int"
; szDescription : LPCSTR optional -> "str"
; dwFlags : DWORD -> "int"
; pbSignature : BYTE* optional, out -> "var"
; pdwSigLen : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptSignHashA(UINT_PTR hHash, DWORD dwKeySpec, LPCSTR szDescription, DWORD dwFlags, BYTE* pbSignature, DWORD* pdwSigLen)
#uselib "ADVAPI32.dll"
#cfunc global CryptSignHashA "CryptSignHashA" intptr, int, str, int, var, var
; res = CryptSignHashA(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
; hHash : UINT_PTR -> "intptr"
; dwKeySpec : DWORD -> "int"
; szDescription : LPCSTR optional -> "str"
; 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")
	procCryptSignHashA = advapi32.NewProc("CryptSignHashA")
)

// hHash (UINT_PTR), dwKeySpec (DWORD), szDescription (LPCSTR optional), dwFlags (DWORD), pbSignature (BYTE* optional, out), pdwSigLen (DWORD* in/out)
r1, _, err := procCryptSignHashA.Call(
	uintptr(hHash),
	uintptr(dwKeySpec),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(szDescription))),
	uintptr(dwFlags),
	uintptr(pbSignature),
	uintptr(pdwSigLen),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptSignHashA(
  hHash: NativeUInt;   // UINT_PTR
  dwKeySpec: DWORD;   // DWORD
  szDescription: PAnsiChar;   // LPCSTR optional
  dwFlags: DWORD;   // DWORD
  pbSignature: Pointer;   // BYTE* optional, out
  pdwSigLen: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CryptSignHashA';
result := DllCall("ADVAPI32\CryptSignHashA"
    , "UPtr", hHash   ; UINT_PTR
    , "UInt", dwKeySpec   ; DWORD
    , "AStr", szDescription   ; LPCSTR optional
    , "UInt", dwFlags   ; DWORD
    , "Ptr", pbSignature   ; BYTE* optional, out
    , "Ptr", pdwSigLen   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CryptSignHashA(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen) = DLL("ADVAPI32.dll", "bool CryptSignHashA(int, dword, char*, dword, void*, void*)")
# 呼び出し: CryptSignHashA(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
# hHash : UINT_PTR -> "int"
# dwKeySpec : DWORD -> "dword"
# szDescription : LPCSTR optional -> "char*"
# dwFlags : DWORD -> "dword"
# pbSignature : BYTE* optional, out -> "void*"
# pdwSigLen : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。