mamcx
mamcx

Reputation: 16186

How generate a MD5 in delphi for azure blob storage?

I try to set a MD5 hash for the files I'm uploading to azure. I get this error:

(400, 'HTTP/1.1 400 The MD5 value specified in the request is a invalid. MD5 value must be 128 bits and base64 encoded.', $7230400)

This is how I do the md5 for the file:

function MD5(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;

......


Headers.Values['Content-MD5'] :=  MD5(LocalFile);

Now, how do this according to azure specs on delphi xe2?

PD: This answer How to resolve an InvalidMd5 error returned from the Windows Azure Blob Storage service? solve it for .NET:

MD5 md5 = new MD5CryptoServiceProvider();
byte[] blockHash = md5.ComputeHash(buff);
string convertedHash = Convert.ToBase64String(blockHash, 0, 16);

But don't know how traslate to delphi..

Upvotes: 2

Views: 1859

Answers (3)

Mick
Mick

Reputation: 856

There are bugs in the Azure code in XE2 you should be aware of (this link also shows how to create and format the MD5 hash value):

EIPHTTPProtocolExceptionPeer exception using PutBlock with array of bytes all set to zero

EDIT:

The relevant code:

Hasher:=TIdHashMessageDigest5.Create;
MD5:=Data.Cloud.CloudAPI.EncodeBytes64(Hasher.HashBytes(Content));
Hasher.Free;

Upvotes: 2

user94559
user94559

Reputation: 60143

I don't know Delphi at all, but the error message says your MD5 hash should be base64 encoded, but it appears you're using hexadecimal encoding.

Upvotes: 1

Brad Phipps
Brad Phipps

Reputation: 46

I don't know about delphi XE2's HashStreamAsHex() function, but in older Delphi versions you did it in two steps: idmd5.AsHex(idmd5.HashValue(fs)) Here's example code for older Delphi versions that you might be able to use. http://delphi.about.com/od/objectpascalide/a/delphi-md5-hash.htm

Upvotes: 1

Related Questions