md5_constexpr.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_HASH_MD5_CONSTEXPR_H_
  5. #define BASE_HASH_MD5_CONSTEXPR_H_
  6. #include "base/hash/md5.h"
  7. #include "base/hash/md5_constexpr_internal.h"
  8. namespace base {
  9. // An constexpr implementation of the MD5 hash function. This is no longer
  10. // considered cryptographically secure, but it is useful as a string hashing
  11. // primitive.
  12. //
  13. // This is not the most efficient implementation, so it is not intended to be
  14. // used at runtime. If you do attempt to use it at runtime you will see
  15. // errors about missing symbols.
  16. // Calculates the MD5 digest of the provided data. When passing |string| with
  17. // no explicit length the terminating null will not be processed.
  18. constexpr MD5Digest MD5SumConstexpr(const char* string);
  19. constexpr MD5Digest MD5SumConstexpr(const char* data, uint32_t length);
  20. // Calculates the first 32/64 bits of the MD5 digest of the provided data,
  21. // returned as a uint32_t/uint64_t. When passing |string| with no explicit
  22. // length the terminating null will not be processed. This abstracts away
  23. // endianness so that the integer will read as the first 4 or 8 bytes of the
  24. // MD5 sum, ensuring that the following outputs are equivalent for
  25. // convenience:
  26. //
  27. // printf("%08x\n", MD5HashConstexpr32("foo"));
  28. //
  29. // MD5Digest d = MD5SumConstexpr("foo");
  30. // printf("%02x%02x%02x%02x\n", d.a[0], d.a[1], d.a[2], d.a[3]);
  31. constexpr uint64_t MD5Hash64Constexpr(const char* string);
  32. constexpr uint64_t MD5Hash64Constexpr(const char* data, uint32_t length);
  33. constexpr uint32_t MD5Hash32Constexpr(const char* string);
  34. constexpr uint32_t MD5Hash32Constexpr(const char* data, uint32_t length);
  35. } // namespace base
  36. #endif // BASE_HASH_MD5_CONSTEXPR_H_