md5.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2011 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_H_
  5. #define BASE_HASH_MD5_H_
  6. #include <string>
  7. #include "base/base_export.h"
  8. #include "base/strings/string_piece.h"
  9. #include "build/build_config.h"
  10. #if defined(OS_NACL)
  11. #include "base/hash/md5_nacl.h"
  12. #else
  13. #include "base/hash/md5_boringssl.h"
  14. #endif
  15. // MD5 stands for Message Digest algorithm 5.
  16. // MD5 is a robust hash function, designed for cyptography, but often used
  17. // for file checksums. The code is complex and slow, but has few
  18. // collisions.
  19. // See Also:
  20. // http://en.wikipedia.org/wiki/MD5
  21. // These functions perform MD5 operations. The simplest call is MD5Sum() to
  22. // generate the MD5 sum of the given data.
  23. //
  24. // You can also compute the MD5 sum of data incrementally by making multiple
  25. // calls to MD5Update():
  26. // MD5Context ctx; // intermediate MD5 data: do not use
  27. // MD5Init(&ctx);
  28. // MD5Update(&ctx, data1, length1);
  29. // MD5Update(&ctx, data2, length2);
  30. // ...
  31. //
  32. // MD5Digest digest; // the result of the computation
  33. // MD5Final(&digest, &ctx);
  34. //
  35. // You can call MD5DigestToBase16() to generate a string of the digest.
  36. namespace base {
  37. // Initializes the given MD5 context structure for subsequent calls to
  38. // MD5Update().
  39. BASE_EXPORT void MD5Init(MD5Context* context);
  40. // For the given buffer of |data| as a StringPiece, updates the given MD5
  41. // context with the sum of the data. You can call this any number of times
  42. // during the computation, except that MD5Init() must have been called first.
  43. BASE_EXPORT void MD5Update(MD5Context* context, const StringPiece& data);
  44. // Finalizes the MD5 operation and fills the buffer with the digest.
  45. BASE_EXPORT void MD5Final(MD5Digest* digest, MD5Context* context);
  46. // Converts a digest into human-readable hexadecimal.
  47. BASE_EXPORT std::string MD5DigestToBase16(const MD5Digest& digest);
  48. // Computes the MD5 sum of the given data buffer with the given length.
  49. // The given 'digest' structure will be filled with the result data.
  50. BASE_EXPORT void MD5Sum(const void* data, size_t length, MD5Digest* digest);
  51. // Returns the MD5 (in hexadecimal) of a string.
  52. BASE_EXPORT std::string MD5String(const StringPiece& str);
  53. } // namespace base
  54. #endif // BASE_HASH_MD5_H_