Delphi 2009 中的 MD5 哈希 [英] MD5 Hashing in Delphi 2009

查看:24
本文介绍了Delphi 2009 中的 MD5 哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 borland delphi 7 甚至在 delphi 2007 中一切正常,但在 delphi 2009 中它只是返回错误的哈希!

In borland delphi 7 and even in delphi 2007 everything worked, but in delphi 2009 it just returns the wrong hash!

我使用 wcrypt2 脚本 (http://pastebin.com/m2f015cfd)

I use wcrypt2 script (http://pastebin.com/m2f015cfd)

看看:

字符串:123456"

string : "123456"

哈希:

Delphi 7:e10adc3949ba59abbe56e057f20f883e"——真实哈希.
Delphi 2007 :e10adc3949ba59abbe56e057f20f883e"——也是真实的哈希值.
和...德尔福 2009 年:5fa285e1bebe0a6623e33afc04a1fbd5"-WTF??

Delphi 7 : "e10adc3949ba59abbe56e057f20f883e" - real hash.
Delphi 2007 : "e10adc3949ba59abbe56e057f20f883e" - real hash too.
And... Delphi 2009 : "5fa285e1bebe0a6623e33afc04a1fbd5" - WTF??

我尝试了很多 md5 脚本,但是 delphi 2009 对所有脚本都做了同样的事情.有什么帮助吗?谢谢.

I've tried a lot of md5 scripts, but delphi 2009 does the same with all of them. Any help? Thanks.

推荐答案

您的库不支持 Unicode.仅仅传递一个 AnsiString 是不够的,因为它可能在内部使用字符串来存储数据.

Your library is not Unicode aware. Just passing it an AnsiString won't be enough because it probably uses strings internally to store data.

您可以尝试更新该库,等待作者更新,或者仅使用 Delphi 2009 附带的 MessageDigest_5.pas.它位于 sourceWin32soapwsdlimporter 文件夹,您需要将其添加到您的路径中,或者将其明确包含在您的项目中.

You could try to update that library, wait for the author to update it, or just use the MessageDigest_5.pas that ships with Delphi 2009. It is in the sourceWin32soapwsdlimporter folder, which you will need to either add to your path, or explicitly include it in your project.

以下是在 Delphi 2009 中使用它的一些示例代码:

Here is some sample code using it in Delphi 2009:

uses Types, MessageDigest_5;

procedure TForm16.Edit1Change(Sender: TObject);
var
  MD5: IMD5;
begin
  MD5 := GetMD5;
  MD5.Init;
  MD5.Update(TByteDynArray(RawByteString(Edit1.Text)), Length(Edit1.Text));
  Edit2.Text := LowerCase(MD5.AsString);
end;

而你在做生意:

MD5(123456) = e10adc3949ba59abbe56e057f20f883e

MD5(123456) = e10adc3949ba59abbe56e057f20f883e

如果您愿意,您可以将其包装在一个简单的函数调用中.在转换为 TByteDynArray 之前转换为 RawByteString 很重要,因为 RawByteString 转换会丢弃所有额外的 Unicode 字符.如果编辑包含 Unicode 字符,那么您最终可能会得到错误的数据.

You could wrap it in a simple function call if you wanted to. It is important you cast to a RawByteString before casting to a TByteDynArray since the RawByteString cast drops all the extra Unicode characters. Granted if the edit contains Unicode characters then you could end up with bad data.

请记住,GetMD5 正在返回一个接口,因此它是引用计数等.

Keep in mind that GetMD5 is returning an interface, so it is reference counted, etc.

圣诞快乐!

这篇关于Delphi 2009 中的 MD5 哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆