CryptSignHashW
関数シグネチャ
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
BOOL CryptSignHashW(
UINT_PTR hHash,
DWORD dwKeySpec,
LPCWSTR szDescription, // optional
DWORD dwFlags,
BYTE* pbSignature, // optional
DWORD* pdwSigLen
);パラメーター
| 名前 | 型 | 方向 | 説明 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| hHash | UINT_PTR | in | 署名対象となるハッシュオブジェクトのハンドルです。 | ||||||||
| dwKeySpec | DWORD | in | プロバイダーのコンテナーから使用する秘密キーを指定します。AT_KEYEXCHANGE または AT_SIGNATURE を指定できます。 使用される署名アルゴリズムは、キーペアを最初に作成したときに指定されます。 Microsoft Base Cryptographic Provider がサポートする署名アルゴリズムは RSA Public Key アルゴリズムのみです。 | ||||||||
| szDescription | LPCWSTR | inoptional | このパラメーターは現在使用されておらず、セキュリティ上の脆弱性を防ぐために NULL に設定する必要があります。ただし、後方互換性のため、Microsoft Base Cryptographic Provider では引き続きサポートされています。 | ||||||||
| dwFlags | DWORD | in | 次のフラグ値が定義されています。
| ||||||||
| pbSignature | BYTE* | outoptional | 署名データを受け取るバッファーへのポインターです。 メモリ割り当ての目的でバッファーサイズを取得するために、このパラメーターに NULL を指定できます。詳細については、 Retrieving Data of Unknown Length を参照してください。 | ||||||||
| pdwSigLen | DWORD* | inout | pbSignature バッファーのサイズ(バイト単位)を指定する DWORD 値へのポインターです。関数が戻ると、この DWORD 値にはバッファーに格納されたバイト数が入ります。 注 バッファーに返されたデータを処理する際、アプリケーションは返されたデータの実際のサイズを使用する必要があります。実際のサイズは、入力時に指定したバッファーのサイズよりわずかに小さくなる場合があります。(入力時、バッファーサイズは通常、想定される最大の出力データが収まるように十分大きく指定します。)出力時には、このパラメーターが指す変数が、バッファーにコピーされたデータの実際のサイズを反映するように更新されます。
|
戻り値の型: BOOL
公式ドキュメント
データに署名します。(CryptSignHashW)
戻り値
関数が成功した場合は TRUE を返します。
関数が失敗した場合は FALSE を返します。拡張エラー情報を取得するには、 GetLastError を呼び出します。
「NTE」で始まるエラーコードは、使用している特定の CSP によって生成されます。考えられるエラーコードを次に示します。
| 戻り値 | 説明 |
|---|---|
| いずれかのパラメーターが有効でないハンドルを指定しています。 | |
| いずれかのパラメーターに有効でない値が含まれています。多くの場合、これは有効でないポインターです。 | |
| pbSignature パラメーターで指定されたバッファーが、返されるデータを保持するのに十分な大きさではありません。必要なバッファーサイズ(バイト単位)は pdwSigLen の DWORD 値に格納されます。 | |
| hHash ハンドルがこの CSP でサポートされていないアルゴリズムを指定しているか、dwKeySpec パラメーターの値が正しくありません。 | |
| dwFlags パラメーターが 0 以外です。 | |
| hHash パラメーターで指定されたハッシュオブジェクトが有効ではありません。 | |
| ハッシュオブジェクトの作成時に指定された CSP コンテキストが見つかりません。 | |
| dwKeySpec で指定された秘密キーが存在しません。 | |
| 操作中に CSP のメモリが不足しました。 |
解説(Remarks)
この関数を呼び出す前に、 CryptCreateHash 関数を呼び出してハッシュオブジェクトのハンドルを取得する必要があります。次に、 CryptHashData または CryptHashSessionKey 関数を使用して、データまたはセッションキーをハッシュオブジェクトに追加します。CryptSignHash 関数はハッシュを完了させます。
DSS CSP は MD5 と SHA の両方のハッシュアルゴリズムによるハッシュ計算をサポートしますが、署名は SHA ハッシュに対してのみサポートします。
この関数を呼び出した後は、ハッシュにこれ以上データを追加できません。CryptHashData または CryptHashSessionKey を追加で呼び出すと失敗します。
アプリケーションがハッシュの使用を終えたら、CryptDestroyHash 関数を呼び出してハッシュオブジェクトを破棄します。
既定では、Microsoft RSA プロバイダーは署名に PKCS #1 パディング方式を使用します。署名の DigestInfo 要素内のハッシュ OID は、ハッシュオブジェクトに関連付けられたアルゴリズム OID に自動的に設定されます。CRYPT_NOHASHOID フラグを使用すると、この OID が署名から省略されます。
まれに、別の場所で生成されたハッシュ値に署名する必要がある場合があります。これは次の一連の操作で行えます。
- CryptCreateHash を使用してハッシュオブジェクトを作成します。
- CryptSetHashParam の dwParam パラメーターに HP_HASHVAL 値を使用して、ハッシュオブジェクトにハッシュ値を設定します。
- CryptSignHash を使用してハッシュ値に署名し、デジタル署名ブロックを取得します。
- CryptDestroyHash を使用してハッシュオブジェクトを破棄します。
例
次の例は、まず署名対象のデータをハッシュし、続いて CryptSignHash 関数を使用してそのハッシュに署名することで、データに署名する方法を示しています。
//-------------------------------------------------------------
// Declare and initialize variables.
HCRYPTPROV hProv;
BYTE *pbBuffer= (BYTE *)"Sample data that is to be signed.";
DWORD dwBufferLen = strlen((char *)pbBuffer)+1;
HCRYPTHASH hHash;
//--------------------------------------------------------------------
// This code assumes that a cryptographic context handle, hProv,
// and a hash handle, hHash, are available.
// For code needed to acquire the context, see "Example C Program:
// Signing a Hash and Verifying the Hash Signature."
//--------------------------------------------------------------------
// Compute the cryptographic hash of the buffer.
if(CryptHashData(
hHash,
pbBuffer,
dwBufferLen,
0))
{
printf("The data buffer has been hashed.\n");
}
else
{
printf("Error during CryptHashData.\n");
exit(1);
}
//--------------------------------------------------------------------
// Determine the size of the signature and allocate memory.
dwSigLen= 0;
if(CryptSignHash(
hHash,
AT_SIGNATURE,
szDescription,
0,
NULL,
&dwSigLen))
{
printf("Signature length %d found.\n",dwSigLen);
}
else
{
printf("Error during CryptSignHash\n");
exit(1);
}
//--------------------------------------------------------------------
// Allocate memory for the signature buffer.
if(pbSignature = (BYTE *)malloc(dwSigLen))
{
printf("Memory allocated for the signature.\n");
}
else
{
printf("Out of memory\n");
exit(1);
}
//--------------------------------------------------------------------
// Sign the hash object.
if(CryptSignHash(
hHash,
AT_SIGNATURE,
szDescription,
0,
pbSignature,
&dwSigLen))
{
printf("pbSignature is the hash signature.\n");
}
else
{
printf("Error during CryptSignHash.\n");
exit(1);
}
//--------------------------------------------------------------------
// Destroy the hash object.
if(hHash)
CryptDestroyHash(hHash);
このコードのコンテキストを含む完全な例については、 Example C Program: Signing a Hash and Verifying the Hash Signature を参照してください。
wincrypt.h ヘッダーは CryptSignHash を、UNICODE プリプロセッサ定数の定義に基づいてこの関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして定義しています。エンコーディング非依存のエイリアスを、エンコーディング非依存でないコードと混在させて使用すると、コンパイルエラーや実行時エラーを引き起こす不一致につながる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 <MarshalAs(UnmanagedType.Bool)> 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 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global CryptSignHashW "CryptSignHashW" sptr, int, wstr, int, sptr, sptr ; res = CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, varptr(pbSignature), varptr(pdwSigLen)) ; hHash : UINT_PTR -> "sptr" ; dwKeySpec : DWORD -> "int" ; szDescription : LPCWSTR optional -> "wstr" ; dwFlags : DWORD -> "int" ; pbSignature : BYTE* optional, out -> "sptr" ; pdwSigLen : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは 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 方式にも切替可。; 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, intptr, intptr ; res = CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, varptr(pbSignature), varptr(pdwSigLen)) ; hHash : UINT_PTR -> "intptr" ; dwKeySpec : DWORD -> "int" ; szDescription : LPCWSTR optional -> "wstr" ; dwFlags : DWORD -> "int" ; pbSignature : BYTE* optional, out -> "intptr" ; pdwSigLen : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは 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 // BOOLfunction 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 版の利用を推奨。const std = @import("std");
extern "advapi32" fn CryptSignHashW(
hHash: usize, // UINT_PTR
dwKeySpec: u32, // DWORD
szDescription: [*c]const u16, // LPCWSTR optional
dwFlags: u32, // DWORD
pbSignature: [*c]u8, // BYTE* optional, out
pdwSigLen: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc CryptSignHashW(
hHash: uint, # UINT_PTR
dwKeySpec: uint32, # DWORD
szDescription: WideCString, # LPCWSTR optional
dwFlags: uint32, # DWORD
pbSignature: ptr uint8, # BYTE* optional, out
pdwSigLen: ptr uint32 # DWORD* in/out
): int32 {.importc: "CryptSignHashW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "advapi32");
extern(Windows)
int CryptSignHashW(
size_t hHash, // UINT_PTR
uint dwKeySpec, // DWORD
const(wchar)* szDescription, // LPCWSTR optional
uint dwFlags, // DWORD
ubyte* pbSignature, // BYTE* optional, out
uint* pdwSigLen // DWORD* in/out
);ccall((:CryptSignHashW, "ADVAPI32.dll"), stdcall, Int32,
(Csize_t, UInt32, Cwstring, UInt32, Ptr{UInt8}, Ptr{UInt32}),
hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
# hHash : UINT_PTR -> Csize_t
# dwKeySpec : DWORD -> UInt32
# szDescription : LPCWSTR optional -> Cwstring
# dwFlags : DWORD -> UInt32
# pbSignature : BYTE* optional, out -> Ptr{UInt8}
# pdwSigLen : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t CryptSignHashW(
uintptr_t hHash,
uint32_t dwKeySpec,
const uint16_t* szDescription,
uint32_t dwFlags,
uint8_t* pbSignature,
uint32_t* pdwSigLen);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
-- hHash : UINT_PTR
-- dwKeySpec : DWORD
-- szDescription : LPCWSTR optional
-- dwFlags : DWORD
-- pbSignature : BYTE* optional, out
-- pdwSigLen : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const CryptSignHashW = lib.func('__stdcall', 'CryptSignHashW', 'int32_t', ['uintptr_t', 'uint32_t', 'str16', 'uint32_t', 'uint8_t *', 'uint32_t *']);
// CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
// hHash : UINT_PTR -> 'uintptr_t'
// dwKeySpec : DWORD -> 'uint32_t'
// szDescription : LPCWSTR optional -> 'str16'
// dwFlags : DWORD -> 'uint32_t'
// pbSignature : BYTE* optional, out -> 'uint8_t *'
// pdwSigLen : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
CryptSignHashW: { parameters: ["usize", "u32", "buffer", "u32", "pointer", "pointer"], result: "i32" },
});
// lib.symbols.CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen)
// hHash : UINT_PTR -> "usize"
// dwKeySpec : DWORD -> "u32"
// szDescription : LPCWSTR optional -> "buffer"
// dwFlags : DWORD -> "u32"
// pbSignature : BYTE* optional, out -> "pointer"
// pdwSigLen : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CryptSignHashW(
size_t hHash,
uint32_t dwKeySpec,
const uint16_t* szDescription,
uint32_t dwFlags,
uint8_t* pbSignature,
uint32_t* pdwSigLen);
C, "ADVAPI32.dll");
// $ffi->CryptSignHashW(hHash, dwKeySpec, szDescription, dwFlags, pbSignature, pdwSigLen);
// hHash : UINT_PTR
// dwKeySpec : DWORD
// szDescription : LPCWSTR optional
// dwFlags : DWORD
// pbSignature : BYTE* optional, out
// pdwSigLen : 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 Advapi32 extends StdCallLibrary {
Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
boolean CryptSignHashW(
long hHash, // UINT_PTR
int dwKeySpec, // DWORD
WString szDescription, // LPCWSTR optional
int dwFlags, // DWORD
byte[] pbSignature, // BYTE* optional, out
IntByReference pdwSigLen // DWORD* in/out
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("advapi32")]
lib LibADVAPI32
fun CryptSignHashW = CryptSignHashW(
hHash : LibC::SizeT, # UINT_PTR
dwKeySpec : UInt32, # DWORD
szDescription : UInt16*, # LPCWSTR optional
dwFlags : UInt32, # DWORD
pbSignature : UInt8*, # BYTE* optional, out
pdwSigLen : 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 CryptSignHashWNative = Int32 Function(UintPtr, Uint32, Pointer<Utf16>, Uint32, Pointer<Uint8>, Pointer<Uint32>);
typedef CryptSignHashWDart = int Function(int, int, Pointer<Utf16>, int, Pointer<Uint8>, Pointer<Uint32>);
final CryptSignHashW = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<CryptSignHashWNative, CryptSignHashWDart>('CryptSignHashW');
// hHash : UINT_PTR -> UintPtr
// dwKeySpec : DWORD -> Uint32
// szDescription : LPCWSTR optional -> Pointer<Utf16>
// dwFlags : DWORD -> Uint32
// pbSignature : BYTE* optional, out -> Pointer<Uint8>
// pdwSigLen : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
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';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CryptSignHashW"
c_CryptSignHashW :: CUIntPtr -> Word32 -> CWString -> Word32 -> Ptr Word8 -> Ptr Word32 -> IO CInt
-- hHash : UINT_PTR -> CUIntPtr
-- dwKeySpec : DWORD -> Word32
-- szDescription : LPCWSTR optional -> CWString
-- dwFlags : DWORD -> Word32
-- pbSignature : BYTE* optional, out -> Ptr Word8
-- pdwSigLen : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cryptsignhashw =
foreign "CryptSignHashW"
(size_t @-> uint32_t @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint8_t) @-> (ptr uint32_t) @-> returning int32_t)
(* hHash : UINT_PTR -> size_t *)
(* dwKeySpec : DWORD -> uint32_t *)
(* szDescription : LPCWSTR optional -> (ptr uint16_t) *)
(* dwFlags : DWORD -> uint32_t *)
(* pbSignature : BYTE* optional, out -> (ptr uint8_t) *)
(* pdwSigLen : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("CryptSignHashW" crypt-sign-hash-w :convention :stdcall) :int32
(h-hash :uint64) ; UINT_PTR
(dw-key-spec :uint32) ; DWORD
(sz-description (:string :encoding :utf-16le)) ; LPCWSTR optional
(dw-flags :uint32) ; DWORD
(pb-signature :pointer) ; BYTE* optional, out
(pdw-sig-len :pointer)) ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CryptSignHashW = Win32::API::More->new('ADVAPI32',
'BOOL CryptSignHashW(WPARAM hHash, DWORD dwKeySpec, LPCWSTR szDescription, DWORD dwFlags, LPVOID pbSignature, LPVOID pdwSigLen)');
# my $ret = $CryptSignHashW->Call($hHash, $dwKeySpec, $szDescription, $dwFlags, $pbSignature, $pdwSigLen);
# hHash : UINT_PTR -> WPARAM
# dwKeySpec : DWORD -> DWORD
# szDescription : LPCWSTR optional -> LPCWSTR
# dwFlags : DWORD -> DWORD
# pbSignature : BYTE* optional, out -> LPVOID
# pdwSigLen : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f CryptSignHashA (ANSI版) — ハッシュ値に秘密鍵でデジタル署名を行う(ANSI版)。
- f CryptCreateHash — 指定アルゴリズムのハッシュオブジェクトを作成する。
- f CryptDestroyHash — ハッシュオブジェクトを破棄する。
- f CryptHashData — ハッシュオブジェクトにデータを追加してハッシュを計算する。
- f CryptHashSessionKey — セッション鍵の内容をハッシュオブジェクトに追加する。
- f CryptVerifySignatureW — 公開鍵を使いハッシュのデジタル署名を検証する(Unicode版)。