我使用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: 0I 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;更多推荐
发布评论