MD5 是目前常用的Hash算法,可以用来校验文件的完整性,或者比对密码等等。本文介绍如何在UEFI下实现计算一个文件MD5的方法。具体算法来自【参考1】,在编译过程中修正了几个关于类型转换的 Warning 。
程序结构很简单,和前一篇关于CRC32的没有多大差别。
#include <Uefi.h> #include <Library/UefiLib.h> #include <Library/ShellCEntryLib.h> #include <stdio.h> #include <stdlib.h> #include <wchar.h> #include <Protocol/EfiShell.h> #include <Library/ShellLib.h> #include <Library/MemoryAllocationLib.h> #include "md5.h" extern EFI_BOOT_SERVICES *gBS; extern EFI_SYSTEM_TABLE *gST; extern EFI_RUNTIME_SERVICES *gRT; extern EFI_SHELL_PROTOCOL *gEfiShellProtocol; int EFIAPI main ( IN int Argc, IN CHAR16 **Argv ) { EFI_FILE_HANDLE FileHandle; RETURN_STATUS Status; EFI_FILE_INFO *FileInfo = NULL; EFI_HANDLE *HandleBuffer=NULL; UINTN ReadSize; MD5_CTX ctx; char R[16]; //Check if there is a parameter if (Argc == 1) { Print(L"Usage: crctest [filename]\n"); return 0; } //Open the file given by the parameter Status = ShellOpenFileByName(Argv[1], (SHELL_FILE_HANDLE *)&FileHandle, EFI_FILE_MODE_READ , 0); if(Status != RETURN_SUCCESS) { Print(L"OpenFile failed!\n"); return EFI_SUCCESS; } //Get file size FileInfo = ShellGetFileInfo( (SHELL_FILE_HANDLE)FileHandle); //Allocate a memory buffer HandleBuffer = AllocateZeroPool((UINTN) FileInfo-> FileSize); if (HandleBuffer == NULL) { return (SHELL_OUT_OF_RESOURCES); } ReadSize=(UINTN) FileInfo-> FileSize; //Load the whole file to the buffer Status = ShellReadFile(FileHandle,&ReadSize,HandleBuffer); MD5_Init(&ctx); MD5_Update(&ctx, HandleBuffer, ReadSize); MD5_Final((unsigned char *)&R, &ctx); //Output Print(L"File Name: %s\n",Argv[1]); Print(L"File Size: %d\n",ReadSize); Print(L"File Size: %d\n",ReadSize); Print(L"MD5 : %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n", R[0]&0xFF ,R[1]&0xFF ,R[2]&0xFF ,R[3]&0xFF , R[4]&0xFF ,R[5]&0xFF ,R[6]&0xFF ,R[7]&0xFF , R[8]&0xFF ,R[9]&0xFF ,R[0xA]&0xFF,R[0xB]&0xFF, R[0xC]&0xFF,R[0xD]&0xFF,R[0xE]&0xFF,R[0xF]&0xFF); FreePool(HandleBuffer); return EFI_SUCCESS; }
运行结果
可以看到,上面的例子能够在Shell下工作正常,但是需要特别注意,我没有验证过 X64 是否正常,如果你有这个需求,请自行验证。
完整的代码下载:
参考:
1.http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 A portable, fast, and free implementation of the MD5 Message-Digest Algorithm (RFC 1321)