| 125 | }; |
| 126 | |
| 127 | class CryptoContext |
| 128 | { |
| 129 | public: |
| 130 | /** Protocol class for a crypto hash context. |
| 131 | |
| 132 | A hash of this type is used for strong hashing, such as for URLs. |
| 133 | */ |
| 134 | class Hasher |
| 135 | { |
| 136 | using self_type = Hasher; ///< Self reference type. |
| 137 | public: |
| 138 | /// Destructor (force virtual) |
| 139 | virtual ~Hasher() {} |
| 140 | /// Update the hash with @a data of @a length bytes. |
| 141 | virtual bool update(void const *data, int length) = 0; |
| 142 | /// Finalize and extract the @a hash. |
| 143 | virtual bool finalize(CryptoHash &hash) = 0; |
| 144 | |
| 145 | /// Convenience overload. |
| 146 | bool finalize(CryptoHash *hash); |
| 147 | |
| 148 | protected: |
| 149 | EVP_MD_CTX *_ctx = nullptr; |
| 150 | }; |
| 151 | |
| 152 | CryptoContext(); |
| 153 | |
| 154 | /// Update the hash with @a data of @a length bytes. |
| 155 | bool update(void const *data, int length); |
| 156 | |
| 157 | /// Convenience - compute final @a hash for @a data. |
| 158 | /// @note This is just as fast as the previous style, as a new context must be initialized |
| 159 | /// every time this is done. |
| 160 | bool hash_immediate(CryptoHash &hash, void const *data, int length); |
| 161 | |
| 162 | /// Finalize and extract the @a hash. |
| 163 | bool finalize(CryptoHash &hash); |
| 164 | |
| 165 | enum HashType { |
| 166 | UNSPECIFIED, |
| 167 | #if TS_ENABLE_FIPS == 0 |
| 168 | MD5, |
| 169 | #endif |
| 170 | SHA256, |
| 171 | }; ///< What type of hash we really are. |
| 172 | static HashType Setting; |
| 173 | |
| 174 | ~CryptoContext(); |
| 175 | |
| 176 | private: |
| 177 | static size_t constexpr OBJ_SIZE = 256; |
| 178 | char _base[OBJ_SIZE]; |
| 179 | }; |
| 180 | |
| 181 | inline bool |
| 182 | CryptoContext::Hasher::finalize(CryptoHash *hash) |
no outgoing calls