1 // Copyright (C) 2024 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ICING_UTIL_SHA256_H_ 16 #define ICING_UTIL_SHA256_H_ 17 18 #include <array> 19 #include <cstddef> 20 #include <cstdint> 21 22 namespace icing { 23 namespace lib { 24 25 class Sha256 { 26 public: 27 Sha256(); 28 29 // Update the SHA256 context with additional data 30 void Update(const uint8_t* data, size_t length); 31 32 // Finalize the SHA256 computation and obtain the 32-byte hash. 33 std::array<uint8_t, 32> Finalize() &&; 34 35 private: 36 // Array to hold the current hash state 37 uint32_t state_[8]; 38 39 // Total number of bytes processed 40 uint64_t count_; 41 42 // The 64-byte buffer to store the input data, sha-256 block size is 64 bytes. 43 std::array<uint8_t, 64> buffer_; 44 45 // Processes a block of input data and updates the hash state. 46 void Transform(); 47 }; 48 49 } // namespace lib 50 } // namespace icing 51 52 #endif // ICING_UTIL_SHA256_H_ 53