/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "perfetto/ext/base/http/sha1.h" #include <stddef.h> #include <string.h> // From chrome_elf/sha1/sha1.cc. namespace perfetto { namespace base { namespace { inline uint32_t BSwap32(uint32_t x) { … } // Usage example: // // SecureHashAlgorithm sha; // while(there is data to hash) // sha.Update(moredata, size of data); // sha.Final(); // memcpy(somewhere, sha.Digest(), 20); // // to reuse the instance of sha, call sha.Init(); class SecureHashAlgorithm { … }; //------------------------------------------------------------------------------ // Private functions //------------------------------------------------------------------------------ // Identifier names follow notation in FIPS PUB 180-3, where you'll // also find a description of the algorithm: // http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf inline uint32_t f(uint32_t t, uint32_t B, uint32_t C, uint32_t D) { … } inline uint32_t S(uint32_t n, uint32_t X) { … } inline uint32_t K(uint32_t t) { … } void SecureHashAlgorithm::Init() { … } void SecureHashAlgorithm::Update(const void* data, size_t nbytes) { … } void SecureHashAlgorithm::Final() { … } void SecureHashAlgorithm::Process() { … } void SecureHashAlgorithm::Pad() { … } // Computes the SHA-1 hash of the |len| bytes in |data| and puts the hash // in |hash|. |hash| must be kSHA1Length bytes long. void SHA1HashBytes(const unsigned char* data, size_t len, unsigned char* hash) { … } } // namespace //------------------------------------------------------------------------------ // Public functions //------------------------------------------------------------------------------ SHA1Digest SHA1Hash(const void* data, size_t size) { … } SHA1Digest SHA1Hash(const std::string& str) { … } } // namespace base } // namespace perfetto