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

CryptHashToBeSigned

関数
エンコードされた証明書の被署名部分のハッシュを計算する。
DLLCRYPT32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptHashToBeSigned(
    HCRYPTPROV_LEGACY hCryptProv,   // optional
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    const BYTE* pbEncoded,
    DWORD cbEncoded,
    BYTE* pbComputedHash,   // optional
    DWORD* pcbComputedHash
);

パラメーター

名前方向説明
hCryptProvHCRYPTPROV_LEGACYinoptional

このパラメーターは使用されず、NULL に設定する必要があります。

Windows Server 2003 および Windows XP: ハッシュの計算に使用する 暗号化サービス プロバイダー (CSP) のハンドルです。このパラメーターのデータ型は HCRYPTPROV です。

hCryptProv で特定の暗号化プロバイダーを渡す明確な理由がない限り、0 を渡します。0 を渡すと、ハッシュ、署名の検証、または受信者の 暗号化 の各操作を行う前に、既定の RSA または Digital Signature Standard (DSS) プロバイダーが取得されます。

dwCertEncodingTypeCERT_QUERY_ENCODING_TYPEin

使用するエンコーディングの種類を指定します。次の例に示すように、証明書メッセージのエンコーディングの種類 の両方をビット単位の OR 演算で組み合わせて指定することは常に可能です。

X509_ASN_ENCODING | PKCS_7_ASN_ENCODING

現在定義されているエンコーディングの種類は次のとおりです。

pbEncodedBYTE*inハッシュ化するコンテンツを格納するバッファーのアドレスです。これは CERT_SIGNED_CONTENT_INFO のエンコードされた形式です。
cbEncodedDWORDinバッファーのサイズ (バイト単位) です。
pbComputedHashBYTE*outoptional

計算されたハッシュを受け取るバッファーへのポインターです。

このパラメーターには、メモリ割り当てのためにこの情報のサイズを取得する目的で NULL を指定できます。詳細については、 Retrieving Data of Unknown Length を参照してください。

pcbComputedHashDWORD*inout

pbComputedHash パラメーターが指すバッファーのサイズ (バイト単位) を格納する DWORD へのポインターです。関数が戻るとき、この DWORD にはバッファーに格納されたバイト数が格納されます。

バッファーで返されるデータを処理する際、アプリケーションは返されたデータの実際のサイズを使用する必要があります。実際のサイズは、入力時に指定したバッファーのサイズよりわずかに小さい場合があります。入力時には、想定される最大の出力データがバッファーに収まるように、通常はバッファー サイズを十分大きく指定します。出力時には、このパラメーターが指す変数が、バッファーにコピーされたデータの実際のサイズを反映するように更新されます。

戻り値の型: BOOL

公式ドキュメント

重要 この API は非推奨です。(CryptHashToBeSigned)

戻り値

関数が成功した場合、関数は 0 以外 (TRUE) を返します。

関数が失敗した場合は 0 (FALSE) を返します。 拡張エラー情報を取得するには、 GetLastError を呼び出します。

呼び出される関数 CryptCreateHashCryptGetHashParam、および CryptHashData からのエラーが、この関数に伝播することがあります。この関数には次のエラー コードがあります。
戻り値 説明
ERROR_MORE_DATA
pbComputedHash パラメーターで指定されたバッファーが、返されるデータを格納するのに十分な大きさでない場合、関数は ERROR_MORE_DATA コードを設定し、必要なバッファー サイズ (バイト単位) を pcbComputedHash が指す変数に格納します。
ERROR_FILE_NOT_FOUND
証明書のエンコーディングの種類が無効です。現在サポートされているのは X509_ASN_ENCODING のみです。
NTE_BAD_ALGID
署名アルゴリズムの オブジェクト識別子 (OID) が、既知またはサポートされているハッシュ アルゴリズムにマップされません。

関数が失敗した場合、GetLastErrorAbstract Syntax Notation One (ASN.1) のエンコード/デコード エラーを返すことがあります。これらのエラーについては、 ASN.1 Encoding/Decoding Return Values を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

BOOL CryptHashToBeSigned(
    HCRYPTPROV_LEGACY hCryptProv,   // optional
    CERT_QUERY_ENCODING_TYPE dwCertEncodingType,
    const BYTE* pbEncoded,
    DWORD cbEncoded,
    BYTE* pbComputedHash,   // optional
    DWORD* pcbComputedHash
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("CRYPT32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptHashToBeSigned(
    UIntPtr hCryptProv,   // HCRYPTPROV_LEGACY optional
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    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 CryptHashToBeSigned(
    hCryptProv As UIntPtr,   ' HCRYPTPROV_LEGACY optional
    dwCertEncodingType As UInteger,   ' CERT_QUERY_ENCODING_TYPE
    pbEncoded As IntPtr,   ' BYTE*
    cbEncoded As UInteger,   ' DWORD
    pbComputedHash As IntPtr,   ' BYTE* optional, out
    ByRef pcbComputedHash As UInteger   ' DWORD* in/out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hCryptProv : HCRYPTPROV_LEGACY optional
' dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
' pbEncoded : BYTE*
' cbEncoded : DWORD
' pbComputedHash : BYTE* optional, out
' pcbComputedHash : DWORD* in/out
Declare PtrSafe Function CryptHashToBeSigned Lib "crypt32" ( _
    ByVal hCryptProv As LongPtr, _
    ByVal dwCertEncodingType 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

CryptHashToBeSigned = ctypes.windll.crypt32.CryptHashToBeSigned
CryptHashToBeSigned.restype = wintypes.BOOL
CryptHashToBeSigned.argtypes = [
    ctypes.c_size_t,  # hCryptProv : HCRYPTPROV_LEGACY optional
    wintypes.DWORD,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    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')
CryptHashToBeSigned = Fiddle::Function.new(
  lib['CryptHashToBeSigned'],
  [
    Fiddle::TYPE_UINTPTR_T,  # hCryptProv : HCRYPTPROV_LEGACY optional
    -Fiddle::TYPE_INT,  # dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
    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 CryptHashToBeSigned(
        hCryptProv: usize,  // HCRYPTPROV_LEGACY optional
        dwCertEncodingType: u32,  // CERT_QUERY_ENCODING_TYPE
        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 CryptHashToBeSigned(UIntPtr hCryptProv, uint dwCertEncodingType, IntPtr pbEncoded, uint cbEncoded, IntPtr pbComputedHash, ref uint pcbComputedHash);
"@
$api = Add-Type -MemberDefinition $sig -Name 'CRYPT32_CryptHashToBeSigned' -Namespace Win32 -PassThru
# $api::CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
#uselib "CRYPT32.dll"
#func global CryptHashToBeSigned "CryptHashToBeSigned" sptr, sptr, sptr, sptr, sptr, sptr
; CryptHashToBeSigned hCryptProv, dwCertEncodingType, varptr(pbEncoded), cbEncoded, varptr(pbComputedHash), varptr(pcbComputedHash)   ; 戻り値は stat
; hCryptProv : HCRYPTPROV_LEGACY optional -> "sptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "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 CryptHashToBeSigned "CryptHashToBeSigned" sptr, int, var, int, var, var
; res = CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
; hCryptProv : HCRYPTPROV_LEGACY optional -> "sptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "int"
; pbEncoded : BYTE* -> "var"
; cbEncoded : DWORD -> "int"
; pbComputedHash : BYTE* optional, out -> "var"
; pcbComputedHash : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CryptHashToBeSigned(HCRYPTPROV_LEGACY hCryptProv, CERT_QUERY_ENCODING_TYPE dwCertEncodingType, BYTE* pbEncoded, DWORD cbEncoded, BYTE* pbComputedHash, DWORD* pcbComputedHash)
#uselib "CRYPT32.dll"
#cfunc global CryptHashToBeSigned "CryptHashToBeSigned" intptr, int, var, int, var, var
; res = CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
; hCryptProv : HCRYPTPROV_LEGACY optional -> "intptr"
; dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "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")
	procCryptHashToBeSigned = crypt32.NewProc("CryptHashToBeSigned")
)

// hCryptProv (HCRYPTPROV_LEGACY optional), dwCertEncodingType (CERT_QUERY_ENCODING_TYPE), pbEncoded (BYTE*), cbEncoded (DWORD), pbComputedHash (BYTE* optional, out), pcbComputedHash (DWORD* in/out)
r1, _, err := procCryptHashToBeSigned.Call(
	uintptr(hCryptProv),
	uintptr(dwCertEncodingType),
	uintptr(pbEncoded),
	uintptr(cbEncoded),
	uintptr(pbComputedHash),
	uintptr(pcbComputedHash),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptHashToBeSigned(
  hCryptProv: NativeUInt;   // HCRYPTPROV_LEGACY optional
  dwCertEncodingType: DWORD;   // CERT_QUERY_ENCODING_TYPE
  pbEncoded: Pointer;   // BYTE*
  cbEncoded: DWORD;   // DWORD
  pbComputedHash: Pointer;   // BYTE* optional, out
  pcbComputedHash: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptHashToBeSigned';
result := DllCall("CRYPT32\CryptHashToBeSigned"
    , "UPtr", hCryptProv   ; HCRYPTPROV_LEGACY optional
    , "UInt", dwCertEncodingType   ; CERT_QUERY_ENCODING_TYPE
    , "Ptr", pbEncoded   ; BYTE*
    , "UInt", cbEncoded   ; DWORD
    , "Ptr", pbComputedHash   ; BYTE* optional, out
    , "Ptr", pcbComputedHash   ; DWORD* in/out
    , "Int")   ; return: BOOL
●CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash) = DLL("CRYPT32.dll", "bool CryptHashToBeSigned(int, dword, void*, dword, void*, void*)")
# 呼び出し: CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
# hCryptProv : HCRYPTPROV_LEGACY optional -> "int"
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "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)。
const std = @import("std");

extern "crypt32" fn CryptHashToBeSigned(
    hCryptProv: usize, // HCRYPTPROV_LEGACY optional
    dwCertEncodingType: u32, // CERT_QUERY_ENCODING_TYPE
    pbEncoded: [*c]u8, // BYTE*
    cbEncoded: u32, // DWORD
    pbComputedHash: [*c]u8, // BYTE* optional, out
    pcbComputedHash: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
proc CryptHashToBeSigned(
    hCryptProv: uint,  # HCRYPTPROV_LEGACY optional
    dwCertEncodingType: uint32,  # CERT_QUERY_ENCODING_TYPE
    pbEncoded: ptr uint8,  # BYTE*
    cbEncoded: uint32,  # DWORD
    pbComputedHash: ptr uint8,  # BYTE* optional, out
    pcbComputedHash: ptr uint32  # DWORD* in/out
): int32 {.importc: "CryptHashToBeSigned", stdcall, dynlib: "CRYPT32.dll".}
pragma(lib, "crypt32");
extern(Windows)
int CryptHashToBeSigned(
    size_t hCryptProv,   // HCRYPTPROV_LEGACY optional
    uint dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
    ubyte* pbEncoded,   // BYTE*
    uint cbEncoded,   // DWORD
    ubyte* pbComputedHash,   // BYTE* optional, out
    uint* pcbComputedHash   // DWORD* in/out
);
ccall((:CryptHashToBeSigned, "CRYPT32.dll"), stdcall, Int32,
      (Csize_t, UInt32, Ptr{UInt8}, UInt32, Ptr{UInt8}, Ptr{UInt32}),
      hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
# hCryptProv : HCRYPTPROV_LEGACY optional -> Csize_t
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> UInt32
# pbEncoded : BYTE* -> Ptr{UInt8}
# cbEncoded : DWORD -> UInt32
# pbComputedHash : BYTE* optional, out -> Ptr{UInt8}
# pcbComputedHash : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t CryptHashToBeSigned(
    uintptr_t hCryptProv,
    uint32_t dwCertEncodingType,
    uint8_t* pbEncoded,
    uint32_t cbEncoded,
    uint8_t* pbComputedHash,
    uint32_t* pcbComputedHash);
]]
local crypt32 = ffi.load("crypt32")
-- crypt32.CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
-- hCryptProv : HCRYPTPROV_LEGACY optional
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
-- pbEncoded : BYTE*
-- cbEncoded : DWORD
-- pbComputedHash : BYTE* optional, out
-- pcbComputedHash : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('CRYPT32.dll');
const CryptHashToBeSigned = lib.func('__stdcall', 'CryptHashToBeSigned', 'int32_t', ['uintptr_t', 'uint32_t', 'uint8_t *', 'uint32_t', 'uint8_t *', 'uint32_t *']);
// CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
// hCryptProv : HCRYPTPROV_LEGACY optional -> 'uintptr_t'
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> 'uint32_t'
// pbEncoded : BYTE* -> 'uint8_t *'
// cbEncoded : DWORD -> 'uint32_t'
// pbComputedHash : BYTE* optional, out -> 'uint8_t *'
// pcbComputedHash : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("CRYPT32.dll", {
  CryptHashToBeSigned: { parameters: ["usize", "u32", "pointer", "u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash)
// hCryptProv : HCRYPTPROV_LEGACY optional -> "usize"
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> "u32"
// pbEncoded : BYTE* -> "pointer"
// cbEncoded : DWORD -> "u32"
// pbComputedHash : BYTE* optional, out -> "pointer"
// pcbComputedHash : DWORD* in/out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t CryptHashToBeSigned(
    size_t hCryptProv,
    uint32_t dwCertEncodingType,
    uint8_t* pbEncoded,
    uint32_t cbEncoded,
    uint8_t* pbComputedHash,
    uint32_t* pcbComputedHash);
C, "CRYPT32.dll");
// $ffi->CryptHashToBeSigned(hCryptProv, dwCertEncodingType, pbEncoded, cbEncoded, pbComputedHash, pcbComputedHash);
// hCryptProv : HCRYPTPROV_LEGACY optional
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE
// pbEncoded : BYTE*
// cbEncoded : DWORD
// pbComputedHash : BYTE* optional, out
// pcbComputedHash : DWORD* in/out
// 構造体/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 Crypt32 extends StdCallLibrary {
    Crypt32 INSTANCE = Native.load("crypt32", Crypt32.class);
    boolean CryptHashToBeSigned(
        long hCryptProv,   // HCRYPTPROV_LEGACY optional
        int dwCertEncodingType,   // CERT_QUERY_ENCODING_TYPE
        byte[] pbEncoded,   // BYTE*
        int cbEncoded,   // DWORD
        byte[] pbComputedHash,   // BYTE* optional, out
        IntByReference pcbComputedHash   // DWORD* in/out
    );
}
@[Link("crypt32")]
lib LibCRYPT32
  fun CryptHashToBeSigned = CryptHashToBeSigned(
    hCryptProv : LibC::SizeT,   # HCRYPTPROV_LEGACY optional
    dwCertEncodingType : UInt32,   # CERT_QUERY_ENCODING_TYPE
    pbEncoded : UInt8*,   # BYTE*
    cbEncoded : UInt32,   # DWORD
    pbComputedHash : UInt8*,   # BYTE* optional, out
    pcbComputedHash : UInt32*   # DWORD* in/out
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CryptHashToBeSignedNative = Int32 Function(UintPtr, Uint32, Pointer<Uint8>, Uint32, Pointer<Uint8>, Pointer<Uint32>);
typedef CryptHashToBeSignedDart = int Function(int, int, Pointer<Uint8>, int, Pointer<Uint8>, Pointer<Uint32>);
final CryptHashToBeSigned = DynamicLibrary.open('CRYPT32.dll')
    .lookupFunction<CryptHashToBeSignedNative, CryptHashToBeSignedDart>('CryptHashToBeSigned');
// hCryptProv : HCRYPTPROV_LEGACY optional -> UintPtr
// dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Uint32
// pbEncoded : BYTE* -> Pointer<Uint8>
// cbEncoded : DWORD -> Uint32
// pbComputedHash : BYTE* optional, out -> Pointer<Uint8>
// pcbComputedHash : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CryptHashToBeSigned(
  hCryptProv: NativeUInt;   // HCRYPTPROV_LEGACY optional
  dwCertEncodingType: DWORD;   // CERT_QUERY_ENCODING_TYPE
  pbEncoded: Pointer;   // BYTE*
  cbEncoded: DWORD;   // DWORD
  pbComputedHash: Pointer;   // BYTE* optional, out
  pcbComputedHash: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'CRYPT32.dll' name 'CryptHashToBeSigned';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CryptHashToBeSigned"
  c_CryptHashToBeSigned :: CUIntPtr -> Word32 -> Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word32 -> IO CInt
-- hCryptProv : HCRYPTPROV_LEGACY optional -> CUIntPtr
-- dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> Word32
-- pbEncoded : BYTE* -> Ptr Word8
-- cbEncoded : DWORD -> Word32
-- pbComputedHash : BYTE* optional, out -> Ptr Word8
-- pcbComputedHash : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let crypthashtobesigned =
  foreign "CryptHashToBeSigned"
    (size_t @-> uint32_t @-> (ptr uint8_t) @-> uint32_t @-> (ptr uint8_t) @-> (ptr uint32_t) @-> returning int32_t)
(* hCryptProv : HCRYPTPROV_LEGACY optional -> size_t *)
(* dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> uint32_t *)
(* pbEncoded : BYTE* -> (ptr uint8_t) *)
(* cbEncoded : DWORD -> uint32_t *)
(* pbComputedHash : BYTE* optional, out -> (ptr uint8_t) *)
(* pcbComputedHash : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library crypt32 (t "CRYPT32.dll"))
(cffi:use-foreign-library crypt32)

(cffi:defcfun ("CryptHashToBeSigned" crypt-hash-to-be-signed :convention :stdcall) :int32
  (h-crypt-prov :uint64)   ; HCRYPTPROV_LEGACY optional
  (dw-cert-encoding-type :uint32)   ; CERT_QUERY_ENCODING_TYPE
  (pb-encoded :pointer)   ; BYTE*
  (cb-encoded :uint32)   ; DWORD
  (pb-computed-hash :pointer)   ; BYTE* optional, out
  (pcb-computed-hash :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CryptHashToBeSigned = Win32::API::More->new('CRYPT32',
    'BOOL CryptHashToBeSigned(WPARAM hCryptProv, DWORD dwCertEncodingType, LPVOID pbEncoded, DWORD cbEncoded, LPVOID pbComputedHash, LPVOID pcbComputedHash)');
# my $ret = $CryptHashToBeSigned->Call($hCryptProv, $dwCertEncodingType, $pbEncoded, $cbEncoded, $pbComputedHash, $pcbComputedHash);
# hCryptProv : HCRYPTPROV_LEGACY optional -> WPARAM
# dwCertEncodingType : CERT_QUERY_ENCODING_TYPE -> DWORD
# pbEncoded : BYTE* -> LPVOID
# cbEncoded : DWORD -> DWORD
# pbComputedHash : BYTE* optional, out -> LPVOID
# pcbComputedHash : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目
使用する型