获取文件的 MD5、CRC、SHA-1、SHA-256、SHA-512

unit untCheckCode;

interface

function GetFile_MD5(const iFileName: String): String;    // 获取文件MD5
function GetFile_CRC(const iFileName: String): String;    // 获取文件CRC
function GetFile_SHA1(const iFileName: String): String;   // 获取文件SHA1
function GetFile_SHA256(const iFileName: String): String; // 获取文件SHA256
function GetFile_SHA384(const iFileName: String): String; // 获取文件SHA384
function GetFile_SHA512(const iFileName: String): String; // 获取文件SHA512

implementation

uses MessageDigest_5, Classes, IdHashCRC, IdHashSHA, IdSSLOpenSSLHeaders;

var
  FOpenSSLLoad: Boolean;

function GetFile_MD5(const iFileName: String): String;
var
  md5       : IMD5;
  fileStream: TMemoryStream;
begin
  fileStream := TMemoryStream.Create;
  try
    fileStream.LoadFromFile(iFileName);
    md5 := GetMD5;
    md5.Init;
    md5.Update(fileStream.Memory, fileStream.Size);
    Result := md5.AsString;
  finally
    fileStream.Free;
  end;
end;

function GetFile_CRC(const iFileName: String): String;
var
  crc       : TIdHashCRC32;
  fileStream: TMemoryStream;
begin
  crc        := TIdHashCRC32.Create;
  fileStream := TMemoryStream.Create;
  try
    fileStream.LoadFromFile(iFileName);
    Result := crc.HashStreamAsHex(fileStream);
  finally
    fileStream.Free;
    crc.Free;
  end;
end;

function GetFile_SHA1(const iFileName: String): String;
var
  SHA1      : TIdHashSHA1;
  fileStream: TMemoryStream;
begin
  SHA1       := TIdHashSHA1.Create;
  fileStream := TMemoryStream.Create;
  try
    fileStream.LoadFromFile(iFileName);
    Result := SHA1.HashStreamAsHex(fileStream);
  finally
    fileStream.Free;
    SHA1.Free;
  end;
end;

function GetFile_SHA256(const iFileName: String): String;
var
  SHA256    : TIdHashSHA256;
  fileStream: TMemoryStream;
begin
  if FOpenSSLLoad then
  begin
    SHA256     := TIdHashSHA256.Create;
    fileStream := TMemoryStream.Create;
    try
      fileStream.LoadFromFile(iFileName);
      Result := SHA256.HashStreamAsHex(fileStream);
    finally
      fileStream.Free;
      SHA256.Free;
    end;
  end
  else
  begin
    Result := 'Error';
  end;
end;

function GetFile_SHA384(const iFileName: String): String;
var
  SHA384    : TIdHashSHA384;
  fileStream: TMemoryStream;
begin
  if FOpenSSLLoad then
  begin
    SHA384     := TIdHashSHA384.Create;
    fileStream := TMemoryStream.Create;
    try
      fileStream.LoadFromFile(iFileName);
      Result := SHA384.HashStreamAsHex(fileStream);
    finally
      fileStream.Free;
      SHA384.Free;
    end;
  end
  else
  begin
    Result := 'Error';
  end;
end;

function GetFile_SHA512(const iFileName: String): String;
var
  SHA512    : TIdHashSHA512;
  fileStream: TMemoryStream;
begin
  if FOpenSSLLoad then
  begin
    SHA512     := TIdHashSHA512.Create;
    fileStream := TMemoryStream.Create;
    try
      fileStream.LoadFromFile(iFileName);
      Result := SHA512.HashStreamAsHex(fileStream);
    finally
      fileStream.Free;
      SHA512.Free;
    end;
  end
  else
  begin
    Result := 'Error';
  end;
end;

initialization

FOpenSSLLoad := IdSSLOpenSSLHeaders.Load();

end.

MessageDigest_5,  IdHashCRC, IdHashSHA, IdSSLOpenSSLHeaders 这些头文件XE2都自带了。任何第三方库都不需要了。



你可能感兴趣的:(获取文件的 MD5、CRC、SHA-1、SHA-256、SHA-512)