| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/common/crypto/md5.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/common/common.hpp" |
| 12 | #include "duckdb/common/types/string_type.hpp" |
| 13 | |
| 14 | namespace duckdb { |
| 15 | |
| 16 | class MD5Context { |
| 17 | public: |
| 18 | static constexpr idx_t MD5_HASH_LENGTH_BINARY = 16; |
| 19 | static constexpr idx_t MD5_HASH_LENGTH_TEXT = 32; |
| 20 | |
| 21 | public: |
| 22 | MD5Context(); |
| 23 | |
| 24 | void Add(const_data_ptr_t data, idx_t len) { |
| 25 | MD5Update(data, len); |
| 26 | } |
| 27 | void Add(const char *data); |
| 28 | void Add(string_t string) { |
| 29 | MD5Update(data: const_data_ptr_cast(src: string.GetData()), len: string.GetSize()); |
| 30 | } |
| 31 | void Add(const string &data) { |
| 32 | MD5Update(data: const_data_ptr_cast(src: data.c_str()), len: data.size()); |
| 33 | } |
| 34 | |
| 35 | //! Write the 16-byte (binary) digest to the specified location |
| 36 | void Finish(data_ptr_t out_digest); |
| 37 | //! Write the 32-character digest (in hexadecimal format) to the specified location |
| 38 | void FinishHex(char *out_digest); |
| 39 | //! Returns the 32-character digest (in hexadecimal format) as a string |
| 40 | string FinishHex(); |
| 41 | |
| 42 | private: |
| 43 | void MD5Update(const_data_ptr_t data, idx_t len); |
| 44 | static void DigestToBase16(const_data_ptr_t digest, char *zBuf); |
| 45 | |
| 46 | uint32_t buf[4]; |
| 47 | uint32_t bits[2]; |
| 48 | unsigned char in[64]; |
| 49 | }; |
| 50 | |
| 51 | } // namespace duckdb |
| 52 |