|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 - Nathanne Isip |
| 3 | + * This file is part of QuickDigest5. |
| 4 | + * |
| 5 | + * N8 is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published |
| 7 | + * by the Free Software Foundation, either version 3 of the License, |
| 8 | + * or (at your option) any later version. |
| 9 | + * |
| 10 | + * N8 is distributed in the hope that it will be useful, but |
| 11 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with N8. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @file QuickDigest5.hpp |
| 21 | + * @author [Nathanne Isip](https://github.com/nthnn) |
| 22 | + * @brief A header file for the QuickDigest5 class, a utility for MD5 hashing. |
| 23 | + * |
| 24 | + * This file defines the `QuickDigest5` class, which provides functionalities |
| 25 | + * for computing MD5 hashes of strings and files. The class also includes |
| 26 | + * static methods for converting the computed hash to a string representation. |
| 27 | + * |
| 28 | + * @note This class is designed as a final class and cannot be inherited. |
| 29 | + */ |
| 30 | +#ifndef QUICKDIGEST5_HPP |
| 31 | +#define QUICKDIGEST5_HPP |
| 32 | + |
| 33 | +#include <cstdint> |
| 34 | +#include <fstream> |
| 35 | +#include <iomanip> |
| 36 | +#include <sstream> |
| 37 | +#include <string> |
| 38 | +#include <vector> |
| 39 | + |
| 40 | +/** |
| 41 | + * @class QuickDigest5 |
| 42 | + * @brief A utility class for MD5 hashing of strings and files. |
| 43 | + * |
| 44 | + * The `QuickDigest5` class provides methods to compute MD5 hashes |
| 45 | + * for input strings and files, as well as converting the hash |
| 46 | + * to a hexadecimal string representation. This class encapsulates |
| 47 | + * the internal MD5 computation logic and exposes static methods |
| 48 | + * for ease of use. |
| 49 | + * |
| 50 | + * @note This class is marked `final` and cannot be subclassed. |
| 51 | + */ |
| 52 | +class QuickDigest5 final { |
| 53 | +private: |
| 54 | + /** |
| 55 | + * @brief Size of the input in bytes. |
| 56 | + */ |
| 57 | + uint64_t size; |
| 58 | + |
| 59 | + /** |
| 60 | + * @brief Buffer for the MD5 computation. |
| 61 | + */ |
| 62 | + std::vector<uint32_t> buffer; |
| 63 | + |
| 64 | + /** |
| 65 | + * @brief Input data to be processed for MD5 hashing. |
| 66 | + */ |
| 67 | + std::vector<uint8_t> input; |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief Stores the resulting MD5 hash. |
| 71 | + */ |
| 72 | + std::vector<uint8_t> digest; |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Padding used during MD5 computation. |
| 76 | + * |
| 77 | + * A static array defining the padding values required |
| 78 | + * to align the input data to a 512-bit boundary. |
| 79 | + */ |
| 80 | + static uint8_t padding[64]; |
| 81 | + |
| 82 | + /** |
| 83 | + * @brief Shift values used during MD5 computation. |
| 84 | + * |
| 85 | + * A static array defining the shift amounts for each |
| 86 | + * step of the MD5 algorithm. |
| 87 | + */ |
| 88 | + static uint32_t shift[64]; |
| 89 | + |
| 90 | + /** |
| 91 | + * @brief Precomputed sine-derived constants. |
| 92 | + * |
| 93 | + * A static array of constants derived from the fractional part |
| 94 | + * of sine values, used in the MD5 algorithm. |
| 95 | + */ |
| 96 | + static uint32_t sineDerivation[64]; |
| 97 | + |
| 98 | + /** |
| 99 | + * @brief Finalizes the MD5 computation. |
| 100 | + * |
| 101 | + * This method performs any final transformations required |
| 102 | + * to complete the MD5 hash computation. |
| 103 | + */ |
| 104 | + void finalize(); |
| 105 | + |
| 106 | + /** |
| 107 | + * @brief Processes a single 512-bit block of input data. |
| 108 | + * |
| 109 | + * @param inputVec A vector containing the input data to process. |
| 110 | + */ |
| 111 | + void step(const std::vector<uint32_t>& inputVec); |
| 112 | + |
| 113 | + /** |
| 114 | + * @brief Updates the internal state with input data. |
| 115 | + * |
| 116 | + * @param inputBuffer A pointer to the input data buffer. |
| 117 | + * @param inputLength The length of the input data in bytes. |
| 118 | + */ |
| 119 | + void update( |
| 120 | + const uint8_t* inputBuffer, |
| 121 | + size_t inputLength |
| 122 | + ); |
| 123 | + |
| 124 | +protected: |
| 125 | + /** |
| 126 | + * @brief Constructs a `QuickDigest5` object. |
| 127 | + * |
| 128 | + * The constructor is protected to prevent direct instantiation. |
| 129 | + * Users should rely on the static methods provided by the class. |
| 130 | + */ |
| 131 | + QuickDigest5(); |
| 132 | + |
| 133 | +public: |
| 134 | + /** |
| 135 | + * @brief Computes the MD5 hash of a string. |
| 136 | + * |
| 137 | + * @param input The input string to hash. |
| 138 | + * @return A vector of bytes representing the MD5 hash. |
| 139 | + */ |
| 140 | + static std::vector<uint8_t> digestString(const std::string& input); |
| 141 | + |
| 142 | + /** |
| 143 | + * @brief Computes the MD5 hash of a file. |
| 144 | + * |
| 145 | + * @param filepath The path to the file to hash. |
| 146 | + * @return A vector of bytes representing the MD5 hash. |
| 147 | + */ |
| 148 | + static std::vector<uint8_t> digestFile(const std::string& filepath); |
| 149 | + |
| 150 | + /** |
| 151 | + * @brief Computes the MD5 hash of a string and returns it as a hex string. |
| 152 | + * |
| 153 | + * @param input The input string to hash. |
| 154 | + * @return A string representing the MD5 hash in hexadecimal format. |
| 155 | + */ |
| 156 | + static std::string toHash(const std::string& input); |
| 157 | + |
| 158 | + /** |
| 159 | + * @brief Computes the MD5 hash of a file and returns it as a hex string. |
| 160 | + * |
| 161 | + * @param filepath The path to the file to hash. |
| 162 | + * @return A string representing the MD5 hash in hexadecimal format. |
| 163 | + */ |
| 164 | + static std::string fileToHash(const std::string& filepath); |
| 165 | +}; |
| 166 | + |
| 167 | +#endif |
0 commit comments