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

CryptHashCertificate

関数
指定アルゴリズムでエンコード済み証明書のハッシュを計算する。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// CRYPT32.dll
#include <windows.h>

BOOL CryptHashCertificate(
    HCRYPTPROV_LEGACY hCryptProv,   // optional
    ALG_ID Algid,
    DWORD dwFlags,
    const BYTE* pbEncoded,
    DWORD cbEncoded,
    BYTE* pbComputedHash,   // optional
    DWORD* pcbComputedHash
);

パラメーター

名前方向
hCryptProvHCRYPTPROV_LEGACYinoptional
AlgidALG_IDin
dwFlagsDWORDin
pbEncodedBYTE*in
cbEncodedDWORDin
pbComputedHashBYTE*outoptional
pcbComputedHashDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

// CRYPT32.dll
#include <windows.h>

BOOL CryptHashCertificate(
    HCRYPTPROV_LEGACY hCryptProv,   // optional
    ALG_ID Algid,
    DWORD dwFlags,
    const BYTE* pbEncoded,
    DWORD cbEncoded,
    BYTE* pbComputedHash,   // optional
    DWORD* pcbComputedHash
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptHashCertificate(
    UIntPtr hCryptProv,   // HCRYPTPROV_LEGACY optional
    uint Algid,   // ALG_ID
    uint dwFlags,   // DWORD
    IntPtr pbEncoded,   // BYTE*
    uint cbEncoded,   // DWORD
    IntPtr pbComputedHash,   // BYTE* optional, out
    ref uint pcbComputedHash   // DWORD* in/out
);
<DllImport("CRYPT32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptHashCertificate(
    hCryptProv As UIntPtr,   ' HCRYPTPROV_LEGACY optional
    Algid As UInteger,   ' ALG_ID
    dwFlags As UInteger,   ' DWORD
    pbEncoded As IntPtr,   ' BYTE*
    cbEncoded As UInteger,   ' DWORD
    pbComputedHash As IntPtr,   ' BYTE* optional, out
    ByRef pcbComputedHash As UInteger   ' DWORD* in/out
) As Boolean
End Function
' hCryptProv : HCRYPTPROV_LEGACY optional
' Algid : ALG_ID
' dwFlags : DWORD
' pbEncoded : BYTE*
' cbEncoded : DWORD
' pbComputedHash : BYTE* optional, out
' pcbComputedHash : DWORD* in/out
Declare PtrSafe Function CryptHashCertificate Lib "crypt32" ( _
    ByVal hCryptProv As LongPtr, _
    ByVal Algid As Long, _
    ByVal dwFlags As Long, _
    ByVal pbEncoded As LongPtr, _
    ByVal cbEncoded As Long, _
    ByVal pbComputedHash As LongPtr, _
    ByRef pcbComputedHash As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptHashCertificate = ctypes.windll.crypt32.CryptHashCertificate
CryptHashCertificate.restype = wintypes.BOOL
CryptHashCertificate.argtypes = [
    ctypes.c_size_t,  # hCryptProv : HCRYPTPROV_LEGACY optional
    wintypes.DWORD,  # Algid : ALG_ID
    wintypes.DWORD,  # dwFlags : DWORD
    ctypes.POINTER(ctypes.c_ubyte),  # pbEncoded : BYTE*
    wintypes.DWORD,  # cbEncoded : DWORD
    ctypes.POINTER(ctypes.c_ubyte),  # pbComputedHash : BYTE* optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcbComputedHash : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('CRYPT32.dll')
CryptHashCertificate = Fiddle::Function.new(
  lib['CryptHashCertificate'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hCryptProv : HCRYPTPROV_LEGACY optional
    -Fiddle::TYPE_INT,  # Algid : ALG_ID
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
    Fiddle::TYPE_VOIDP,  # pbEncoded : BYTE*
    -Fiddle::TYPE_INT,  # cbEncoded : DWORD
    Fiddle::TYPE_VOIDP,  # pbComputedHash : BYTE* optional, out
    Fiddle::TYPE_VOIDP,  # pcbComputedHash : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "crypt32")]
extern "system" {
    fn CryptHashCertificate(
        hCryptProv: usize,  // HCRYPTPROV_LEGACY optional
        Algid: u32,  // ALG_ID
        dwFlags: u32,  // DWORD
        pbEncoded: *const u8,  // BYTE*
        cbEncoded: u32,  // DWORD
        pbComputedHash: *mut u8,  // BYTE* optional, out
        pcbComputedHash: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true)]
public static extern bool CryptHashCertificate(UIntPtr hCryptProv, uint Algid, uint dwFlags, IntPtr pbEncoded, uint cbEncoded, IntPtr pbComputedHash, ref uint pcbComputedHash);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptHashCertificate' -Namespace Win32 -PassThru
# $api::CryptHashCertificate(hCryptProv, Algid, dwFlags, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
#uselib "CRYPT32.dll"
#func global CryptHashCertificate "CryptHashCertificate" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CryptHashCertificate hCryptProv, Algid, dwFlags, varptr(pbEncoded), cbEncoded, varptr(pbComputedHash), varptr(pcbComputedHash)   ; 戻り値は stat
; hCryptProv : HCRYPTPROV_LEGACY optional -> "sptr"
; Algid : ALG_ID -> "sptr"
; dwFlags : DWORD -> "sptr"
; pbEncoded : BYTE* -> "sptr"
; cbEncoded : DWORD -> "sptr"
; pbComputedHash : BYTE* optional, out -> "sptr"
; pcbComputedHash : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "CRYPT32.dll"
#cfunc global CryptHashCertificate "CryptHashCertificate" sptr, int, int, var, int, var, var
; res = CryptHashCertificate(hCryptProv, Algid, dwFlags, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
; hCryptProv : HCRYPTPROV_LEGACY optional -> "sptr"
; Algid : ALG_ID -> "int"
; dwFlags : DWORD -> "int"
; pbEncoded : BYTE* -> "var"
; cbEncoded : DWORD -> "int"
; pbComputedHash : BYTE* optional, out -> "var"
; pcbComputedHash : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptHashCertificate(HCRYPTPROV_LEGACY hCryptProv, ALG_ID Algid, DWORD dwFlags, BYTE* pbEncoded, DWORD cbEncoded, BYTE* pbComputedHash, DWORD* pcbComputedHash)
#uselib "CRYPT32.dll"
#cfunc global CryptHashCertificate "CryptHashCertificate" intptr, int, int, var, int, var, var
; res = CryptHashCertificate(hCryptProv, Algid, dwFlags, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
; hCryptProv : HCRYPTPROV_LEGACY optional -> "intptr"
; Algid : ALG_ID -> "int"
; dwFlags : DWORD -> "int"
; pbEncoded : BYTE* -> "var"
; cbEncoded : DWORD -> "int"
; pbComputedHash : BYTE* optional, out -> "var"
; pcbComputedHash : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	crypt32 = windows.NewLazySystemDLL("CRYPT32.dll")
	procCryptHashCertificate = crypt32.NewProc("CryptHashCertificate")
)

// hCryptProv (HCRYPTPROV_LEGACY optional), Algid (ALG_ID), dwFlags (DWORD), pbEncoded (BYTE*), cbEncoded (DWORD), pbComputedHash (BYTE* optional, out), pcbComputedHash (DWORD* in/out)
r1, _, err := procCryptHashCertificate.Call(
	uintptr(hCryptProv),
	uintptr(Algid),
	uintptr(dwFlags),
	uintptr(pbEncoded),
	uintptr(cbEncoded),
	uintptr(pbComputedHash),
	uintptr(pcbComputedHash),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptHashCertificate(
  hCryptProv: NativeUInt;   // HCRYPTPROV_LEGACY optional
  Algid: DWORD;   // ALG_ID
  dwFlags: DWORD;   // DWORD
  pbEncoded: Pointer;   // BYTE*
  cbEncoded: DWORD;   // DWORD
  pbComputedHash: Pointer;   // BYTE* optional, out
  pcbComputedHash: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptHashCertificate';
result := DllCall("CRYPT32\CryptHashCertificate"
    , "UPtr", hCryptProv   ; HCRYPTPROV_LEGACY optional
    , "UInt", Algid   ; ALG_ID
    , "UInt", dwFlags   ; DWORD
    , "Ptr", pbEncoded   ; BYTE*
    , "UInt", cbEncoded   ; DWORD
    , "Ptr", pbComputedHash   ; BYTE* optional, out
    , "Ptr", pcbComputedHash   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CryptHashCertificate(hCryptProv, Algid, dwFlags, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash) = DLL("CRYPT32.dll", "bool CryptHashCertificate(int, dword, dword, void*, dword, void*, void*)")
# 呼び出し: CryptHashCertificate(hCryptProv, Algid, dwFlags, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
# hCryptProv : HCRYPTPROV_LEGACY optional -> "int"
# Algid : ALG_ID -> "dword"
# dwFlags : DWORD -> "dword"
# pbEncoded : BYTE* -> "void*"
# cbEncoded : DWORD -> "dword"
# pbComputedHash : BYTE* optional, out -> "void*"
# pcbComputedHash : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。