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

CryptDuplicateHash

関数
ハッシュオブジェクトを複製して状態をコピーする。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL CryptDuplicateHash(
    UINT_PTR hHash,
    DWORD* pdwReserved,   // optional
    DWORD dwFlags,
    UINT_PTR* phHash
);

パラメーター

名前方向説明
hHashUINT_PTRin複製するハッシュのハンドル。
pdwReservedDWORD*optional将来の使用のために予約されており、0 を指定する必要があります。
dwFlagsDWORDin将来の使用のために予約されており、0 を指定する必要があります。
phHashUINT_PTR*out複製されたハッシュのハンドルのアドレス。ハッシュの使用が終わったら、CryptDestroyHash 関数を呼び出してハンドルを解放してください。

戻り値の型: BOOL

公式ドキュメント

ハッシュを、複製を行った時点までの状態で正確に複製します。

戻り値

関数が成功すると、TRUE を返します。

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

「NTE」で始まるエラーコードは、使用している特定の 暗号化サービスプロバイダー (CSP) によって生成されます。考えられるエラーコードの一部を以下に示します。

戻り値 説明
ERROR_CALL_NOT_IMPLEMENTED
これは新しい関数であるため、既存の CSP では実装できません。CSP がこの関数をサポートしていない場合に、このエラーが返されます。
ERROR_INVALID_PARAMETER
いずれかのパラメーターに無効な値が含まれています。これは多くの場合、無効なポインターです。
NTE_BAD_HASH
元のハッシュのハンドルが無効です。

解説(Remarks)

CryptDuplicateHash は、ハッシュと、そのハッシュの正確な状態のコピーを作成します。この関数は、呼び出し元アプリケーションが 2 つのハッシュを生成する必要があり、かつ両方のハッシュが共通のデータをハッシュした状態から開始しなければならない場合に使用できます。たとえば、ハッシュを作成して共通データをハッシュし、CryptDuplicateHash 関数で複製を作成してから、各ハッシュに固有のデータを追加する、といった使い方ができます。

CryptDuplicateHash で作成したハッシュを破棄するには、CryptDestroyHash 関数を呼び出す必要があります。元のハッシュを破棄しても、複製されたハッシュは破棄されません。複製されたハッシュは、作成された時点で元のハッシュとは別個のものになります。2 つのハッシュの間で共有される状態はありません。

次の例は、ハッシュの正確なコピーを作成する方法を示しています。この例の完全なコンテキストを含む例については、C プログラムの例: ハッシュの複製を参照してください。

//-------------------------------------------------------------------
//  Declare and initialize variables.

HCRYPTPROV   hCryptProv = NULL;
HCRYPTHASH   hOriginalHash = NULL;
HCRYPTHASH   hDuplicateHash = NULL;

//-------------------------------------------------------------------
// Acquire a CSP context.

if(CryptAcquireContext(
   &hCryptProv, 
   NULL, 
   NULL, 
   PROV_RSA_FULL, 
   0)) 
{
    printf("CryptAcquireContext succeeded. \n");
}
else
{
    printf("Error during CryptAcquireContext.\n");
    exit(1);
}
//-------------------------------------------------------------------
// Create a hash.

if (CryptCreateHash(
    hCryptProv, 
    CALG_SHA1, 
    0, 
    0,
    &hOriginalHash))
{
   printf("An empty hash object has been created. \n");
}
else
{
   printf("Error during CryptCreateHash.\n");
   exit(1);
}
//-------------------------------------------------------------------
// Hash a BYTE string.

if (CryptHashData(
    hOriginalHash, 
    (BYTE*)"Some Common Data", 
    sizeof("Some Common Data"), 0))
{
   printf("An original hash has been created. \n");
}
else
{
   printf("Error during CryptHashData.\n");
   exit(1);
}
//-------------------------------------------------------------------
// Duplicate the hash.

if (CryptDuplicateHash(
   hOriginalHash, 
   NULL, 
   0, 
   &hDuplicateHash))
{
   printf("The hash has been duplicated. \n");
}
else
{
   printf("Error during CryptDuplicateHash.\n");
   exit(1);
}
//-------------------------------------------------------------------
// At this point, the two hash objects are exactly the same.
// The two hash objects can be handled separately.
// When all processing on the hash object is completed, 
// both objects should be destroyed, and the cryptographic
// context should be released.

//-------------------------------------------------------------------
// Destroy the original hash.

if(CryptDestroyHash(hOriginalHash))
{
   printf("The original hash has been destroyed. \n");
}
else
{
   printf("Error during CryptDestroyHash on the original "
       "hash object.\n");
   exit(1);
}
//-------------------------------------------------------------------
// Destroy the duplicate hash.

if (CryptDestroyHash(hDuplicateHash))
{
   printf("The duplicate hash has been destroyed. \n");
}
else
{
   printf("Error during CryptDestroyHash on the duplicated hash object.\n");
   exit(1);
}

//-------------------------------------------------------------------
// Release the CSP.

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

各言語での呼び出し定義

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

BOOL CryptDuplicateHash(
    UINT_PTR hHash,
    DWORD* pdwReserved,   // optional
    DWORD dwFlags,
    UINT_PTR* phHash
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CryptDuplicateHash(
    UIntPtr hHash,   // UINT_PTR
    IntPtr pdwReserved,   // DWORD* optional
    uint dwFlags,   // DWORD
    out UIntPtr phHash   // UINT_PTR* out
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CryptDuplicateHash(
    hHash As UIntPtr,   ' UINT_PTR
    pdwReserved As IntPtr,   ' DWORD* optional
    dwFlags As UInteger,   ' DWORD
    <Out> ByRef phHash As UIntPtr   ' UINT_PTR* out
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hHash : UINT_PTR
' pdwReserved : DWORD* optional
' dwFlags : DWORD
' phHash : UINT_PTR* out
Declare PtrSafe Function CryptDuplicateHash Lib "advapi32" ( _
    ByVal hHash As LongPtr, _
    ByVal pdwReserved As LongPtr, _
    ByVal dwFlags As Long, _
    ByRef phHash As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CryptDuplicateHash = ctypes.windll.advapi32.CryptDuplicateHash
CryptDuplicateHash.restype = wintypes.BOOL
CryptDuplicateHash.argtypes = [
    ctypes.c_size_t,  # hHash : UINT_PTR
    ctypes.POINTER(wintypes.DWORD),  # pdwReserved : DWORD* optional
    wintypes.DWORD,  # dwFlags : DWORD
    ctypes.POINTER(ctypes.c_size_t),  # phHash : UINT_PTR* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCryptDuplicateHash = advapi32.NewProc("CryptDuplicateHash")
)

// hHash (UINT_PTR), pdwReserved (DWORD* optional), dwFlags (DWORD), phHash (UINT_PTR* out)
r1, _, err := procCryptDuplicateHash.Call(
	uintptr(hHash),
	uintptr(pdwReserved),
	uintptr(dwFlags),
	uintptr(phHash),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CryptDuplicateHash(
  hHash: NativeUInt;   // UINT_PTR
  pdwReserved: Pointer;   // DWORD* optional
  dwFlags: DWORD;   // DWORD
  phHash: Pointer   // UINT_PTR* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CryptDuplicateHash';
result := DllCall("ADVAPI32\CryptDuplicateHash"
    , "UPtr", hHash   ; UINT_PTR
    , "Ptr", pdwReserved   ; DWORD* optional
    , "UInt", dwFlags   ; DWORD
    , "Ptr", phHash   ; UINT_PTR* out
    , "Int")   ; return: BOOL
●CryptDuplicateHash(hHash, pdwReserved, dwFlags, phHash) = DLL("ADVAPI32.dll", "bool CryptDuplicateHash(int, void*, dword, void*)")
# 呼び出し: CryptDuplicateHash(hHash, pdwReserved, dwFlags, phHash)
# hHash : UINT_PTR -> "int"
# pdwReserved : DWORD* optional -> "void*"
# dwFlags : DWORD -> "dword"
# phHash : UINT_PTR* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

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

typedef CryptDuplicateHashNative = Int32 Function(UintPtr, Pointer<Uint32>, Uint32, Pointer<UintPtr>);
typedef CryptDuplicateHashDart = int Function(int, Pointer<Uint32>, int, Pointer<UintPtr>);
final CryptDuplicateHash = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<CryptDuplicateHashNative, CryptDuplicateHashDart>('CryptDuplicateHash');
// hHash : UINT_PTR -> UintPtr
// pdwReserved : DWORD* optional -> Pointer<Uint32>
// dwFlags : DWORD -> Uint32
// phHash : UINT_PTR* out -> Pointer<UintPtr>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CryptDuplicateHash(
  hHash: NativeUInt;   // UINT_PTR
  pdwReserved: Pointer;   // DWORD* optional
  dwFlags: DWORD;   // DWORD
  phHash: Pointer   // UINT_PTR* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CryptDuplicateHash';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CryptDuplicateHash"
  c_CryptDuplicateHash :: CUIntPtr -> Ptr Word32 -> Word32 -> Ptr CUIntPtr -> IO CInt
-- hHash : UINT_PTR -> CUIntPtr
-- pdwReserved : DWORD* optional -> Ptr Word32
-- dwFlags : DWORD -> Word32
-- phHash : UINT_PTR* out -> Ptr CUIntPtr
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let cryptduplicatehash =
  foreign "CryptDuplicateHash"
    (size_t @-> (ptr uint32_t) @-> uint32_t @-> (ptr size_t) @-> returning int32_t)
(* hHash : UINT_PTR -> size_t *)
(* pdwReserved : DWORD* optional -> (ptr uint32_t) *)
(* dwFlags : DWORD -> uint32_t *)
(* phHash : UINT_PTR* out -> (ptr size_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("CryptDuplicateHash" crypt-duplicate-hash :convention :stdcall) :int32
  (h-hash :uint64)   ; UINT_PTR
  (pdw-reserved :pointer)   ; DWORD* optional
  (dw-flags :uint32)   ; DWORD
  (ph-hash :pointer))   ; UINT_PTR* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CryptDuplicateHash = Win32::API::More->new('ADVAPI32',
    'BOOL CryptDuplicateHash(WPARAM hHash, LPVOID pdwReserved, DWORD dwFlags, LPVOID phHash)');
# my $ret = $CryptDuplicateHash->Call($hHash, $pdwReserved, $dwFlags, $phHash);
# hHash : UINT_PTR -> WPARAM
# pdwReserved : DWORD* optional -> LPVOID
# dwFlags : DWORD -> DWORD
# phHash : UINT_PTR* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目