rvargas@google.com | c3b6518 | 2011-03-29 08:48:44 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
fbarchard@chromium.org | 0613fdd | 2009-10-09 05:05:01 +0900 | [diff] [blame] | 5 | #ifndef BASE_MD5_H_ |
| 6 | #define BASE_MD5_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 8 | #include "base/base_export.h" |
tfarina@chromium.org | b6d4911 | 2013-03-30 23:29:00 +0900 | [diff] [blame] | 9 | #include "base/strings/string_piece.h" |
rvargas@google.com | c3b6518 | 2011-03-29 08:48:44 +0900 | [diff] [blame] | 10 | |
tfarina@chromium.org | 20d046d | 2011-07-19 01:54:58 +0900 | [diff] [blame] | 11 | namespace base { |
| 12 | |
fbarchard@chromium.org | 0613fdd | 2009-10-09 05:05:01 +0900 | [diff] [blame] | 13 | // MD5 stands for Message Digest algorithm 5. |
| 14 | // MD5 is a robust hash function, designed for cyptography, but often used |
| 15 | // for file checksums. The code is complex and slow, but has few |
| 16 | // collisions. |
| 17 | // See Also: |
| 18 | // http://en.wikipedia.org/wiki/MD5 |
| 19 | |
| 20 | // These functions perform MD5 operations. The simplest call is MD5Sum() to |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 21 | // generate the MD5 sum of the given data. |
| 22 | // |
| 23 | // You can also compute the MD5 sum of data incrementally by making multiple |
fbarchard@chromium.org | 0613fdd | 2009-10-09 05:05:01 +0900 | [diff] [blame] | 24 | // calls to MD5Update(): |
| 25 | // MD5Context ctx; // intermediate MD5 data: do not use |
| 26 | // MD5Init(&ctx); |
| 27 | // MD5Update(&ctx, data1, length1); |
| 28 | // MD5Update(&ctx, data2, length2); |
| 29 | // ... |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 30 | // |
fbarchard@chromium.org | 0613fdd | 2009-10-09 05:05:01 +0900 | [diff] [blame] | 31 | // MD5Digest digest; // the result of the computation |
| 32 | // MD5Final(&digest, &ctx); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 33 | // |
fbarchard@chromium.org | 0613fdd | 2009-10-09 05:05:01 +0900 | [diff] [blame] | 34 | // You can call MD5DigestToBase16() to generate a string of the digest. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 35 | |
tfarina@chromium.org | b64a8a7 | 2011-07-15 11:53:37 +0900 | [diff] [blame] | 36 | // The output of an MD5 operation. |
| 37 | struct MD5Digest { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 38 | unsigned char a[16]; |
tfarina@chromium.org | b64a8a7 | 2011-07-15 11:53:37 +0900 | [diff] [blame] | 39 | }; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 40 | |
| 41 | // Used for storing intermediate data during an MD5 computation. Callers |
| 42 | // should not access the data. |
| 43 | typedef char MD5Context[88]; |
| 44 | |
| 45 | // Computes the MD5 sum of the given data buffer with the given length. |
| 46 | // The given 'digest' structure will be filled with the result data. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 47 | BASE_EXPORT void MD5Sum(const void* data, size_t length, MD5Digest* digest); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 48 | |
| 49 | // Initializes the given MD5 context structure for subsequent calls to |
fbarchard@chromium.org | 0613fdd | 2009-10-09 05:05:01 +0900 | [diff] [blame] | 50 | // MD5Update(). |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 51 | BASE_EXPORT void MD5Init(MD5Context* context); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 52 | |
dominich@chromium.org | f03d694 | 2011-07-27 08:39:50 +0900 | [diff] [blame] | 53 | // For the given buffer of |data| as a StringPiece, updates the given MD5 |
| 54 | // context with the sum of the data. You can call this any number of times |
| 55 | // during the computation, except that MD5Init() must have been called first. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 56 | BASE_EXPORT void MD5Update(MD5Context* context, const StringPiece& data); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 57 | |
| 58 | // Finalizes the MD5 operation and fills the buffer with the digest. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 59 | BASE_EXPORT void MD5Final(MD5Digest* digest, MD5Context* context); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 60 | |
shess@chromium.org | 7f164d9 | 2014-04-01 10:31:37 +0900 | [diff] [blame] | 61 | // MD5IntermediateFinal() generates a digest without finalizing the MD5 |
| 62 | // operation. Can be used to generate digests for the input seen thus far, |
| 63 | // without affecting the digest generated for the entire input. |
| 64 | BASE_EXPORT void MD5IntermediateFinal(MD5Digest* digest, |
| 65 | const MD5Context* context); |
| 66 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 67 | // Converts a digest into human-readable hexadecimal. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 68 | BASE_EXPORT std::string MD5DigestToBase16(const MD5Digest& digest); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 69 | |
| 70 | // Returns the MD5 (in hexadecimal) of a string. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 71 | BASE_EXPORT std::string MD5String(const StringPiece& str); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 72 | |
tfarina@chromium.org | 20d046d | 2011-07-19 01:54:58 +0900 | [diff] [blame] | 73 | } // namespace base |
| 74 | |
fbarchard@chromium.org | 0613fdd | 2009-10-09 05:05:01 +0900 | [diff] [blame] | 75 | #endif // BASE_MD5_H_ |