2

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..

Community
  • 1
  • 1
mamcx
  • 15,916
  • 26
  • 101
  • 189
  • If you were to dump your data to a file, and then use one of the free MD5 calculators, do you get the same answer or not? I'd start there, before trying to troubleshoot your calculator, as there may be nothing wrong with your calculation. i.e. you're assuming that Azure doesn't like your MD5, but maybe maybe Azure doesn't like your file. – Chris Thornton Apr 20 '12 at 17:48
  • I get identical result testing with osx md5 command line against the same file. – mamcx Apr 20 '12 at 19:04

3 Answers3

2

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;
Community
  • 1
  • 1
Mick
  • 846
  • 2
  • 7
  • 18
1

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

1

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.

user94559
  • 59,196
  • 6
  • 103
  • 103