1

Here is my sample code:

        auto cClient = client.GetBlobContainerClient(cName);
        auto bClient = cClient.GetBlockBlobClient(bName);

        auto res = bClient.UploadFrom(reinterpret_cast<const std::uint8_t *>(buffer), bufferSize);

        Azure::Core::Cryptography::Md5Hash md5Hash;
        md5Hash.Append(reinterpret_cast<const std::uint8_t *>(buffer), bufferSize);

        if(md5Hash.Final() != res.Value.TransactionalContentHash.Value().Value){
            std::cout << "Not equal " << std::endl;
        }else{
            std::cout << "Equal " << std::endl;
        }

I want to verify md5 hash of uploaded file. I've read the documentation and it's little confusing. It says that you can calculate md5 of file locally and somehow set it to the request and Azure on the server side will calculate md5 on it's own and compare it to the one you provided and will fail if they don't match.

I've also read that you receive md5 as a response from Azure and you can compare it locally. I have 2 questions here:

  1. With the code above I'm trying calculate hash of file locally and compare it to the one provided by Azure, but for some reason they are always not equal, Am I missing something?
  2. How can I provide md5 of a file to the upload function so that Azure fails to upload if they don't match?

Thanks

Jakomo
  • 71
  • 6
  • Probably need to see how you are creating `buffer` and `bufferSize`. – Andy Oct 25 '22 at 12:48
  • They are basically arguments to this function `const char *buffer, std::uint64_t bufferSize` – Jakomo Oct 25 '22 at 12:50
  • Right, but you could be calculating `buffer` or `bufferSize` incorrectly. Typically, you should be showing a Minimal Reproducible example. Something someone could copy and paste and see exactly what you are seeing. Right now, half the code is missing, so we won't be able to reproduce what you are seeing. – Andy Oct 25 '22 at 12:55
  • I think more important question is second one, how to provide md5 to upload request? – Jakomo Oct 25 '22 at 12:56
  • What is the type and value of `res.Value.TransactionalContentHash.Value().Value` and `md5Hash.Final()`? Are you comparing them correctly? Have you tried to print them out? – thedemons Oct 25 '22 at 14:08
  • Are we seriously still trusting MD5 in 2022? – tadman Oct 25 '22 at 14:10
  • @thedemons yes I tried to print it but it's bunch of non ascii symbols, I tried to convert it to string since values of those are `uint8_t` like `std::str(res.Value.TransactionalContentHash.Value().Value.begin(), res.Value.TransactionalContentHash.Value().Value.end())` but still it prints some non ascii stuff – Jakomo Oct 25 '22 at 14:24
  • @tadman what other options do I have with Azure api? – Jakomo Oct 25 '22 at 14:25
  • @Jakomo I believe `md5Hash.Final()` return a `uint8_t*` to an array that has a size of 32. You're comparing the address of two arrays therefore it is always not equal. Convert the array into hex string and print them out. You could use `memcmp` to compare them. – thedemons Oct 25 '22 at 14:28
  • This is more of a reflection on how outdated Azure is than your code. I'm just stunned. – tadman Oct 25 '22 at 14:29
  • @thedemons `std::vector Final() { return Final(nullptr, 0); }` It returns the array – Jakomo Oct 25 '22 at 14:30
  • @thedemons I actually printed the data and yes they are different for some reason, `md5Hash.Final` is two times bigger than one returned `res.Value.TransactionalContentHash.Value()` – Jakomo Oct 25 '22 at 15:09
  • @Jakomo two times bigger in size? Md5 hashes should be 32 bytes in length – thedemons Oct 25 '22 at 15:15
  • These were the values `\x09\xf7\xe0\x2f\x12\x90\xbe\x21\x1d\xa7\x07\xa2\x66\xf1\x53\xb3` and `\x7e\xb9\xed\x8b\x61\x10\x92\x21` – Jakomo Oct 25 '22 at 15:17
  • @thedemons Used https://stackoverflow.com/questions/26503606/better-way-to-convert-a-vector-of-uint8-to-an-ascii-hexadecimal-string function to print data – Jakomo Oct 25 '22 at 15:27

0 Answers0