This is an archive of the discontinued LLVM Phabricator instance.

[Support] Update `MD5` to follow other hashes.
ClosedPublic

Authored by arames on Aug 9 2021, 1:32 PM.

Details

Summary

Introduce StringRef final() and StringRef result().

Diff Detail

Event Timeline

arames created this revision.Aug 9 2021, 1:32 PM
arames requested review of this revision.Aug 9 2021, 1:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 9 2021, 1:32 PM
dexonsmith requested changes to this revision.Aug 16 2021, 1:24 PM

Sorry I missed this before -- I have a few suggestions below to align this better with SHA1 and SHA256.

llvm/include/llvm/Support/MD5.h
93–95

I'm not seeing finalWords() in the other hashers. I suggest not adding it.

97

The other hashers return a StringRef. MD5 should align with that, and also return the raw bytes as they do (instead of a hex string).

Looking at SHA1 and SHA256, they have two methods, result() and final(), the former of which can be called at any time. Here is the implementation of result():

StringRef SHA1::result() {
  auto StateToRestore = InternalState;

  auto Hash = final();

  // Restore the state
  InternalState = StateToRestore;

  // Return pointer to hash (20 characters)
  return Hash;
}

I think if we're adding one we should add both, for consistency.

  • Seems we'd need a prep NFC patch that moved its members into an InternalState member.
  • MD5::final() can add an MD5Result member, calling final(MD5Result&) on it and returning a StringRef referencing MD5Result::bytes, and then MD5::result() can be implemented exactly as in SHA1.
llvm/lib/Support/MD5.cpp
276

The other hashers do not return a digest, but the raw bytes. MD5 should align with that.

llvm/unittests/Support/MD5Test.cpp
77

I think this should avoid a magic number, and just confirm that the final() return is equal to MD5Result::bytes when calling final(MD5Result&).

This revision now requires changes to proceed.Aug 16 2021, 1:24 PM
arames updated this revision to Diff 367578.Aug 19 2021, 12:00 PM
arames marked 3 inline comments as done.

Address review comments.

arames marked an inline comment as done.Aug 19 2021, 12:00 PM
arames retitled this revision from [Support] Introduce `SmallString<32> MD5::final()`... to [Support] Update `MD5` to follow other hashes..Aug 19 2021, 12:22 PM
arames edited the summary of this revision. (Show Details)
This revision is now accepted and ready to land.Aug 19 2021, 1:20 PM
This revision was landed with ongoing or failed builds.Aug 19 2021, 2:13 PM
This revision was automatically updated to reflect the committed changes.