文件 MD5 校验和

发布于 2024-07-11 13:12:10 字数 215 浏览 9 评论 0原文

这个问题中提到了wcrypt2。

我需要的只是计算一个文件的MD5。 如果我可以计算它而不需要保存它那就完美了,因为它是流格式的下载文件。

我希望有最直接的方法来做到这一点。

谢谢!

In this question is mentioned the wcrypt2.

What I need is simply calculate the MD5 of a file. It would be perfect if I could calculate it without having to save it because it is a downloaded file in stream format.

I would like to have the most straightforward way to do that.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

献世佛 2024-07-18 13:12:11

这是 Indy 10 的工作代码:

function MD5File(const FileName: string): string;
var
  IdMD5: TIdHashMessageDigest5;
  FS: TFileStream;
begin
 IdMD5 := TIdHashMessageDigest5.Create;
 FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
 try
   Result := IdMD5.HashStreamAsHex(FS)
 finally
   FS.Free;
   IdMD5.Free;
 end;
end;

问候,
奥斯卡R1

Here is a working code for Indy 10:

function MD5File(const FileName: string): string;
var
  IdMD5: TIdHashMessageDigest5;
  FS: TFileStream;
begin
 IdMD5 := TIdHashMessageDigest5.Create;
 FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
 try
   Result := IdMD5.HashStreamAsHex(FS)
 finally
   FS.Free;
   IdMD5.Free;
 end;
end;

Regards,
OscaR1

终弃我 2024-07-18 13:12:11

基于@dummzeuch 的回答,我写了这个函数:

function getMD5checksum(s: TStream): string;
 var
  md5: TIdHashMessageDigest5;
  hash : T4x4LongWordRecord;
 begin
  md5 := TIdHashMessageDigest5.Create;
  s.Seek(0,0);
  hash := md5.HashValue(s);
  result := IntToHex(Integer(hash[0]), 4) +
            IntToHex(Integer(hash[1]), 4) +
            IntToHex(Integer(hash[2]), 4) +
            IntToHex(Integer(hash[3]), 4);
 end;

Based on @dummzeuch answere I wrote this function:

function getMD5checksum(s: TStream): string;
 var
  md5: TIdHashMessageDigest5;
  hash : T4x4LongWordRecord;
 begin
  md5 := TIdHashMessageDigest5.Create;
  s.Seek(0,0);
  hash := md5.HashValue(s);
  result := IntToHex(Integer(hash[0]), 4) +
            IntToHex(Integer(hash[1]), 4) +
            IntToHex(Integer(hash[2]), 4) +
            IntToHex(Integer(hash[3]), 4);
 end;
一个人练习一个人 2024-07-18 13:12:11

Indy 附带了计算多种哈希值的函数,MD5 就是其中之一。 至少从 Delphi 2006 开始,Indy 就包含在所有版本的 Delphi 中,并且可以免费下载旧版本。

Indy comes with functions for calculating several hashes, MD5 is one of them. Indy is included in all versions of Delphi since at least Delphi 2006 and available as a free download for older versions.

荒岛晴空 2024-07-18 13:12:11

关于什么:

function GetFileMD5(const Stream: TStream): String; overload;
var MD5: TIdHashMessageDigest5;
begin
    MD5 := TIdHashMessageDigest5.Create;
    try
       Result := MD5.HashStreamAsHex(Stream);
    finally
       MD5.Free;
    end;
end;

function GetFileMD5(const Filename: String): String; overload;
var FileStream: TFileStream;
begin
    FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
    try
      Result := GetFileMD5(FileStream);
    finally
      FileStream.Free;
    end;
end;

What about:

function GetFileMD5(const Stream: TStream): String; overload;
var MD5: TIdHashMessageDigest5;
begin
    MD5 := TIdHashMessageDigest5.Create;
    try
       Result := MD5.HashStreamAsHex(Stream);
    finally
       MD5.Free;
    end;
end;

function GetFileMD5(const Filename: String): String; overload;
var FileStream: TFileStream;
begin
    FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
    try
      Result := GetFileMD5(FileStream);
    finally
      FileStream.Free;
    end;
end;
在你怀里撒娇 2024-07-18 13:12:11

正如您提到的,您链接到的帖子讨论了wcrypt2,这是一个加密例程库,包括 MD5。 您链接到的帖子似乎也表明它可用于 Delphi 7,因为询问者包含标记为“Delphi 7”的输出。 您已将此问题标记为 delphi7,因此我假设您也正在使用该版本。 那么是什么阻止您使用 wcrypt2 呢?

该问题链接到 wcrypt2.pas 的副本,该文件中的版权日期似乎表明该单元在 Delphi 7 发布时可用。 检查您的安装; 你可能已经拥有了。 如果没有,那么该单位还说它是通过Project Jedi获得的,所以你可以尝试看看单位也有。

您所引用问题的答案包括示例 Delphi 代码以及 Delphi 附带的用于执行 MD5 的单元名称。 它们随 Delphi 2009 一起提供,因此您应该检查它们是否也适用于您的版本。

As you mentioned, the post you linked to talks about wcrypt2, which is a library of cryptographic routines, including MD5. The post you linked to also seems to indicate that it is available for Delphi 7 since the asker includes output labeled "Delphi 7." You have tagged this question delphi7, so I assume that's the version you're using, too. So what's stopping you from using wcrypt2?

The question links to a copy of wcrypt2.pas, and the copyright dates in that file appear to indicate that the unit was available by the time Delphi 7 was released. Check your installation; you might already have it. If not, then the unit also says that it was obtained via Project Jedi, so you could try looking there for the unit as well.

The answers to your referenced question include example Delphi code and the names of units that come with Delphi for doing MD5. They come with Delphi 2009, so you should check whether they're also available for your version.

是你 2024-07-18 13:12:11

看一下 Delphi 中 MD5SUM 的这个实现。 它需要一个字符串作为输入,但我想你可以轻松地使其与流一起工作。

Take a look at this implementation of MD5SUM in Delphi. It requires a string for input, but I imagine you can easily make it work with a stream.

吐个泡泡 2024-07-18 13:12:11

MessageDigest_5 也适用于此。

MessageDigest_5 would work for this as well.

无法回应 2024-07-18 13:12:11

我在 Delphi 7 和 Indy 10.1.5 中使用以下函数

uses IdHashMessageDigest, idHash, Classes;  

...

function cc_MD5File(const p_fileName : string) : string;
//returns MD5 has for a file
var
  v_idmd5 : TIdHashMessageDigest5;
  v_fs : TFileStream;
  v_hash : T4x4LongWordRecord;
begin
  v_idmd5 := TIdHashMessageDigest5.Create;
  v_fs := TFileStream.Create(p_fileName, fmOpenRead OR fmShareDenyWrite) ;
  try
    v_hash := v_idmd5.HashValue(v_fs);
    result := v_idmd5.AsHex(v_hash);
  finally
    v_fs.Free;
    v_idmd5.Free;
  end;
end;

I use the following function in Delphi 7 with Indy 10.1.5

uses IdHashMessageDigest, idHash, Classes;  

...

function cc_MD5File(const p_fileName : string) : string;
//returns MD5 has for a file
var
  v_idmd5 : TIdHashMessageDigest5;
  v_fs : TFileStream;
  v_hash : T4x4LongWordRecord;
begin
  v_idmd5 := TIdHashMessageDigest5.Create;
  v_fs := TFileStream.Create(p_fileName, fmOpenRead OR fmShareDenyWrite) ;
  try
    v_hash := v_idmd5.HashValue(v_fs);
    result := v_idmd5.AsHex(v_hash);
  finally
    v_fs.Free;
    v_idmd5.Free;
  end;
end;
情痴 2024-07-18 13:12:11

如果您使用 Overbyte http://www.overbyte.eu/frame_index.html 只需添加单位并使用文件名调用函数 FileMD5

uses OverbyteIcsMd5;
....
function GetMd5File:String; 
begin
 Result := FileMD5(FileName);
end;

If you use Overbyte http://www.overbyte.eu/frame_index.html just add unit and call function FileMD5 with name of file

uses OverbyteIcsMd5;
....
function GetMd5File:String; 
begin
 Result := FileMD5(FileName);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文