1 // Copyright 2019 The Chromium OS 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 5 // Note: this is the leveraged design from Chromium library src/base/hash/md5.h 6 7 #ifndef C2_E2E_TEST_MD5_H_ 8 #define C2_E2E_TEST_MD5_H_ 9 10 #include <stddef.h> 11 #include <stdint.h> 12 13 #include <string> 14 15 namespace android { 16 17 // MD5 stands for Message Digest algorithm 5. 18 // MD5 is a robust hash function, designed for cyptography, but often used 19 // for file checksums. The code is complex and slow, but has few 20 // collisions. 21 // See Also: 22 // http://en.wikipedia.org/wiki/MD5 23 24 // These functions perform MD5 operations. The simplest call is MD5Sum() to 25 // generate the MD5 sum of the given data. 26 // 27 // You can also compute the MD5 sum of data incrementally by making multiple 28 // calls to MD5Update(): 29 // MD5Context ctx; // intermediate MD5 data: do not use 30 // MD5Init(&ctx); 31 // MD5Update(&ctx, data1, length1); 32 // MD5Update(&ctx, data2, length2); 33 // ... 34 // 35 // MD5Digest digest; // the result of the computation 36 // MD5Final(&digest, &ctx); 37 // 38 // You can call MD5DigestToBase16() to generate a string of the digest. 39 40 // The output of an MD5 operation. 41 struct MD5Digest { 42 uint8_t a[16]; 43 }; 44 45 // Used for storing intermediate data during an MD5 computation. Callers 46 // should not access the data. 47 typedef char MD5Context[88]; 48 49 // Initializes the given MD5 context structure for subsequent calls to 50 // MD5Update(). 51 void MD5Init(MD5Context* context); 52 53 // For the given buffer of |data| as a string, 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. 56 void MD5Update(MD5Context* context, const std::string& data); 57 58 // Finalizes the MD5 operation and fills the buffer with the digest. 59 void MD5Final(MD5Digest* digest, MD5Context* context); 60 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 void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context); 65 66 // Converts a digest into human-readable hexadecimal. 67 std::string MD5DigestToBase16(const MD5Digest& digest); 68 69 // Computes the MD5 sum of the given data buffer with the given length. 70 // The given 'digest' structure will be filled with the result data. 71 void MD5Sum(const void* data, size_t length, MD5Digest* digest); 72 73 // Returns the MD5 (in hexadecimal) of a string. 74 std::string MD5String(const std::string& str); 75 76 } // namespace android 77 78 #endif // C2_E2E_TEST_MD5_H_ 79