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