Data.Cloud.CloudAPI.pas
पर एक गलत SHA256 लौटाता हैहै जो कुछ फ़ाइल पर गलत SHA 256 लौटाता है।डेल्फी THashSHA2 बड़ी फ़ाइल
class function TCloudSHA256Authentication.GetStreamToHashSHA256Hex(const Content: TStream): string;
var
LBytes : TBytes;
Hash: THashSHA2;
begin
LBytes := TBytesStream(Content).Bytes;
//Hash bytes
Hash := THashSHA2.Create;
Hash.Update(LBytes);
Result := Hash.HashAsString;
end;
एडब्ल्यूएस S3 वापसी त्रुटि: कुछ परीक्षण
<ClientComputedContentSHA256>f43ee89e2b7758057bb1f33eb8546d4c2c118f2ab932de89dbd74aabc0651053</ClientComputedContentSHA256>
<S3ComputedContentSHA256>3bbf5f864cc139cf6392b4623bd782a69d16929db713bffaa68035f8a5c3c0ce</S3ComputedContentSHA256>
मैं बना दिया है एक myfile.zip (600 एमबी) बुद्धि:
The provided x-amz-content-sha256 header does not match what was computed
GetStreamToHashSHA256Hex अमेज़न से एक अलग SHA256 उत्पादन लगता है। ..
TIdHashSHA256
इंडी से एक विकल्प सही SHA256 (aws s3 के समान) लौटाता है, उदाहरण के लिए ।:
var
aFileStream: TFileStream;
aHash: TIdHashSHA256;
begin
aFileStream := TFileStream.Create('C:\myfile.zip', fmOpenRead or fmShareDenyWrite);
aHash := TIdHashSHA256.Create;
try
Result := aHash.HashStreamAsHex(aFileStream).ToLower;
finally
aFileStream.Free;
aHash.Free;
end;
end;
PHP से hash_file()
सही SHA256, जैसे .:
hash_file('sha256', 'C:\myfile.zip');
(एडब्ल्यूएस S3 का एक ही) लौट लेकिन THashSHA2 एक गलत SHA256 वापसी, जैसे .:
var
LBytes : TBytes;
Hash: THashSHA2;
begin
LBytes := TFile.ReadAllBytes('C:\myfile.zip');
Hash := THashSHA2.Create;
Hash.Update(LBytes);
Result := Hash.HashAsString;
end;
क्यूं कर?
अद्यतन
यह मेरा बग समाधान है। परियोजना में आयात Data.Cloud.CloudAPI.pas और पुनर्लेखन इन समारोह:
uses IdHash, IdHashSHA, IdSSLOpenSSL;
class function TCloudSHA256Authentication.GetHashSHA256Hex(HashString: string): string;
var
aHash: TIdHashSHA256;
begin
LoadOpenSSLLibrary;
try
if not(TIdHashSHA256.IsAvailable) then
raise Exception.Create('HashSHA256 Isn''t available!');
aHash := TIdHashSHA256.Create;
try
Result := aHash.HashStringAsHex(HashString).ToLower;
finally
aHash.Free;
end;
finally
UnLoadOpenSSLLibrary;
end;
end;
class function TCloudSHA256Authentication.GetStreamToHashSHA256Hex(const Content: TStream): string;
var
aHash: TIdHashSHA256;
begin
LoadOpenSSLLibrary;
try
if not(TIdHashSHA256.IsAvailable) then
raise Exception.Create('HashSHA256 Isn''t available!');
aHash := TIdHashSHA256.Create;
try
Result := aHash.HashStreamAsHex(Content).ToLower;
finally
aHash.Free;
end;
finally
UnLoadOpenSSLLibrary;
end;
end;
अद्यतन 2
मैं है भी Freds सुझाव को लागू करने की कोशिश करते हैं, यह काम करता है:
class function TCloudSHA256Authentication.GetHashSHA256Hex(HashString: string): string;
var
Content: TStringStream;
Hash: THashSHA2;
LBytes: TArray<Byte>;
Buffer: PByte;
BufLen: Integer;
Readed: Integer;
begin
BufLen := 16 * 1024;
Buffer := AllocMem(BufLen);
Hash := THashSHA2.Create;
Content := TStringStream.Create(HashString);
try
while Content.Position < Content.Size do
begin
Readed := Content.Read(Buffer^, BufLen);
if Readed > 0 then
Hash.update(Buffer^, Readed);
end;
finally
Content.Free;
FreeMem(Buffer);
end;
Result := Hash.HashAsString;
end;
class function TCloudSHA256Authentication.GetStreamToHashSHA256Hex(const Content: TStream): string;
var
LBytes : TBytes;
Hash: THashSHA2;
Buffer: PByte;
BufLen: Integer;
Readed: Integer;
begin
BufLen := 16 * 1024;
Buffer := AllocMem(BufLen);
Hash := THashSHA2.Create;
try
Content.Seek(0, soFromBeginning);
while Content.Position < Content.Size do
begin
Readed := Content.Read(Buffer^, BufLen);
if Readed > 0 then
Hash.update(Buffer^, Readed);
end;
Content.Seek(0, soFromBeginning);
finally
FreeMem(Buffer);
end;
Result := Hash.HashAsString;
end;
जाहिर 'THashSHA2' गाड़ी तो है। Embarcadero के साथ कृपया [एक बग रिपोर्ट दर्ज करें] (http://quality.embarcadero.com)। –
@RemyLebeau THashSHA2 छोटी गाड़ी है, क्या आप इसे ठीक करने के लिए कोई कामकाज जानते हैं? – ar099968
कई सही कार्यान्वयन हैं जिनका आप उपयोग कर सकते हैं। ढूँढो एक। इसका इस्तेमाल करें। –