在Delphi中获取PDF MD5哈希值(Get PDF MD5 Hash in Delphi)

编程入门 行业动态 更新时间:2024-10-26 21:24:34
在Delphi中获取PDF MD5哈希值(Get PDF MD5 Hash in Delphi)

我使用Delphi从pdf文件生成哈希MD5。 我想生成与CouchDB附件哈希相同的哈希,但我得到不同的结果。

我生成哈希的函数:

function TPliki.generujHashMD5(zawartoscPliku: TMemoryStream): string; var tekstPliku: string; begin setString(tekstPliku, PAnsiChar(zawartoscPliku.Memory), zawartoscPliku.Size); result := THashMD5.GetHashString(tekstPliku); end; 在函数之前,我使用TMemoryStream.LoadFromFile(path)将文件加载到TMemoryStream中, 调用这个函数TPliki.GenerujHashMD5(fileContent):string; 在这个函数中,我生成哈希并返回结果字符串。 当我从Base64 CouchDB Hash解码时,我的哈希值不同。

CouchDB for Attachments中的设置:

compressible_types: text/*, application/javascript, application/json, application/xml compression_level: 0

I am using Delphi to generate hash MD5 from pdf file. I would like to generate the same hash as CouchDB attachment hash, but I get different result.

My function to generate Hash:

function TPliki.generujHashMD5(zawartoscPliku: TMemoryStream): string; var tekstPliku: string; begin setString(tekstPliku, PAnsiChar(zawartoscPliku.Memory), zawartoscPliku.Size); result := THashMD5.GetHashString(tekstPliku); end; Before function I load file to TMemoryStream using TMemoryStream.LoadFromFile(path) Call this function TPliki.GenerujHashMD5(fileContent): string; In this function I generate Hash and return string with result. My hash is different when I decode from Base64 CouchDB Hash.

Settings in CouchDB for Attachments:

compressible_types: text/*, application/javascript, application/json, application/xml compression_level: 0

最满意答案

您的代码问题在于您正在将PDF文件的内容 - 二进制数据转换为Unicode字符串,并且您正在改变其内容。 这会改变你的哈希值。

您应该以不同的方式使用THashMD5 ,并直接提供二进制数据而不将其转换为字符串。

function TPliki.generujHashMD5(zawartoscPliku: TMemoryStream): string; var hash: THashMD5; begin hash := THashMD5.Create; hash.Update(zawartoscPliku.Memory, zawartoscPliku.Size); result := hash.HashAsString; end;

Problem with your code is that you are converting content of PDF file - binary data to Unicode string and with that you are altering its content. And that changes your hash value.

You should be using THashMD5 in different way, and supply binary data directly without converting it to string.

function TPliki.generujHashMD5(zawartoscPliku: TMemoryStream): string; var hash: THashMD5; begin hash := THashMD5.Create; hash.Update(zawartoscPliku.Memory, zawartoscPliku.Size); result := hash.HashAsString; end;

更多推荐

本文发布于:2023-08-04 23:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PDF   Delphi   Hash   哈希值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!