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