CryptCreateHash + CALG_MD5
发布日期:2021-06-30 22:10:27 浏览次数:2 分类:技术文章

本文共 5043 字,大约阅读时间需要 16 分钟。

前言

看见反汇编中有一段算md5的代码,记录一下。

记录

// testCase1.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include 
#include
#include
#include
BOOL __cdecl fnMd5(const BYTE* pbData, DWORD dwDataLen, BYTE* pucAryHash);int main(int argc, char* argv[]){ int i = 0; BYTE cBuf[MAXBYTE] = {
'\0'}; ///< 全0的buffer BYTE ucRc[16] = {
'\0'}; if (fnMd5(cBuf, MAXBYTE, ucRc)) { for (i = 0; i < sizeof(ucRc); i++) { printf("%.2X ", ucRc[i]); } printf("\r\n"); } /** run result 6D F9 01 2B 2B 7C B3 C5 59 63 49 9A 26 30 9B BA */ printf("END\n"); return 0;}BOOL __cdecl fnMd5(const BYTE* pbData, DWORD dwDataLen, BYTE* pucAryHash){ BOOL bRc = FALSE; // 这里直接假设算出的hash值为16字节 // msdn上的说法是要先算一下hash值占用的空间, CryptGetHashParam(hHash, HP_HASHSIZE DWORD dwOutPutBufferSize = 16; HCRYPTPROV hProv = NULL; HCRYPTHASH hHash = NULL; if (!CryptAcquireContextA(&hProv, 0, 0, PROV_RSA_FULL/*1u*/, CRYPT_VERIFYCONTEXT/*0xF0000000u*/)) { return 0; } if (CryptCreateHash(hProv, CALG_MD5/*0x8003u*/, 0, 0, &hHash)) { if (!CryptHashData(hHash, pbData, dwDataLen, 0)) { CryptReleaseContext(hProv, 0); CryptDestroyHash(hHash); return 0; } if (CryptGetHashParam(hHash, HP_HASHVAL/*2u*/, pucAryHash, &dwOutPutBufferSize, 0)) { bRc = TRUE; } CryptDestroyHash(hHash); } CryptReleaseContext(hProv, 0); return bRc;}

反汇编代码

; =============== S U B R O U T I N E =======================================; Attributes: bp-based frame; int __cdecl fnMd5(BYTE *pbData, DWORD dwDataLen, int)fnMd5           proc near               ; CODE XREF: fnMd51+68p fnMd52+18p ...pdwDataLen      = dword ptr -0ChhProv           = dword ptr -8hHash           = dword ptr -4pbData          = dword ptr  8dwDataLen       = dword ptr  0Charg_8           = dword ptr  10h                push    ebp                mov     ebp, esp                sub     esp, 0Ch                push    esi                push    edi                xor     esi, esi                push    0F0000000h      ; dwFlags                push    1               ; dwProvType                push    esi             ; szProvider                lea     eax, [ebp+hProv]                push    esi             ; szContainer                push    eax             ; phProv                xor     edi, edi                mov     [ebp+hProv], esi                mov     [ebp+hHash], esi                mov     [ebp+pdwDataLen], esi                call    CryptAcquireContextA                test    eax, eax                jz      short loc_10004896                lea     eax, [ebp+hHash]                push    eax             ; phHash                push    esi             ; dwFlags                push    esi             ; hKey                push    8003h           ; Algid                push    [ebp+hProv]     ; hProv                call    CryptCreateHash                test    eax, eax                jz      short loc_100048C4                push    esi             ; dwFlags                push    [ebp+dwDataLen] ; dwDataLen                push    [ebp+pbData]    ; pbData                push    [ebp+hHash]     ; hHash                call    CryptHashData                test    eax, eax                jnz     short loc_1000489A                push    esi             ; dwFlags                push    [ebp+hProv]     ; hProv                call    CryptReleaseContext                push    [ebp+hHash]     ; hHash                call    CryptDestroyHashloc_10004896:                           ; CODE XREF: fnMd5+2Aj                xor     eax, eax                jmp     short loc_100048D0; ---------------------------------------------------------------------------loc_1000489A:                           ; CODE XREF: fnMd5+56j                lea     eax, [ebp+pdwDataLen]                push    esi             ; dwFlags                push    eax             ; pdwDataLen                mov     [ebp+pdwDataLen], 10h                push    [ebp+arg_8]     ; pbData                push    2               ; dwParam                push    [ebp+hHash]     ; hHash                call    CryptGetHashParam                test    eax, eax                jz      short loc_100048BB                push    1                pop     ediloc_100048BB:                           ; CODE XREF: fnMd5+8Bj                push    [ebp+hHash]     ; hHash                call    CryptDestroyHashloc_100048C4:                           ; CODE XREF: fnMd5+42j                push    esi             ; dwFlags                push    [ebp+hProv]     ; hProv                call    CryptReleaseContext                mov     eax, ediloc_100048D0:                           ; CODE XREF: fnMd5+6Dj                pop     edi                pop     esi                leave                retnfnMd5           endp; =============== S U B R O U T I N E =======================================

转载地址:https://lostspeed.blog.csdn.net/article/details/52585050 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:类内存布局中的虚表项空间占用
下一篇:get c_function* from lua_State*

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月29日 20时41分15秒