/** * @brief MD5 HASH digest compute example. * @param InputMessage: pointer to input message to be hashed. * @param InputMessageLength: input data message length in byte. * @param MessageDigest: pointer to output parameter that will handle message digest * @param MessageDigestLength: pointer to output digest length. * @retval error status: can be HASH_SUCCESS if success or one of * HASH_ERR_BAD_PARAMETER, HASH_ERR_BAD_CONTEXT, * HASH_ERR_BAD_OPERATION if error occured. */ int32_t STM32_MD5_HASH_DigestCompute(uint8_t* InputMessage, uint32_t InputMessageLength, uint8_t *MessageDigest, int32_t* MessageDigestLength) { MD5ctx_stt P_pMD5ctx; uint32_t error_status = HASH_SUCCESS; /* Set the size of the desired hash digest */ P_pMD5ctx.mTagSize = CRL_MD5_SIZE; /* Set flag field to default value */ P_pMD5ctx.mFlags = E_HASH_DEFAULT; error_status = MD5_Init(&P_pMD5ctx); /* check for initialization errors */ if (error_status == HASH_SUCCESS) { /* Add data to be hashed */ error_status = MD5_Append(&P_pMD5ctx, InputMessage, InputMessageLength); if (error_status == HASH_SUCCESS) { /* retrieve */ error_status = MD5_Finish(&P_pMD5ctx, MessageDigest, MessageDigestLength); } } return error_status; }
BOOL WINAPI SFileVerifyFile(HANDLE hMpq, const char * szFileName, DWORD dwFlags) { crc32_context crc32_ctx; md5_context md5_ctx; TMPQFile * hf; TMPQCRC32 Crc32; TMPQMD5 Md5; BYTE Buffer[0x1000]; HANDLE hFile = NULL; DWORD dwBytesRead; BOOL bResult = TRUE; // Attempt to open the file if(SFileOpenFileEx(hMpq, szFileName, 0, &hFile)) { // Initialize the CRC32 and MD5 counters CRC32_Init(&crc32_ctx); MD5_Init(&md5_ctx); hf = (TMPQFile *)hFile; // Go through entire file and update both CRC32 and MD5 for(;;) { // Read data from file SFileReadFile(hFile, Buffer, sizeof(Buffer), &dwBytesRead, NULL); if(dwBytesRead == 0) break; // Update CRC32 value if(dwFlags & MPQ_ATTRIBUTE_CRC32) CRC32_Update(&crc32_ctx, Buffer, (int)dwBytesRead); // Update MD5 value if(dwFlags & MPQ_ATTRIBUTE_MD5) MD5_Update(&md5_ctx, Buffer, (int)dwBytesRead); } // Check if the CRC32 matches if((dwFlags & MPQ_ATTRIBUTE_CRC32) && hf->pCrc32 != NULL) { CRC32_Finish(&crc32_ctx, (unsigned long *)&Crc32.dwValue); if(Crc32.dwValue != hf->pCrc32->dwValue) bResult = FALSE; } // Check if MD5 matches if((dwFlags & MPQ_ATTRIBUTE_MD5) && hf->pMd5 != NULL) { MD5_Finish(&md5_ctx, Md5.Value); if(memcmp(Md5.Value, hf->pMd5->Value, sizeof(TMPQMD5))) bResult = FALSE; } SFileCloseFile(hFile); } return bResult; }