1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 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 LIBPANDABASE_UTILS_HASH_BASE_H_ 16 #define LIBPANDABASE_UTILS_HASH_BASE_H_ 17 18 #include <array> 19 #include <cstdint> 20 #include <cstdlib> 21 #include <macros.h> 22 23 namespace panda { 24 25 // Superclass for all hash classes. Defines interfaces for hash methods. 26 template <typename HashImpl> 27 class HashBase { 28 public: 29 /** 30 * \brief Create 32 bits Hash from \param key via \param seed. 31 * @param key - a key which should be hashed 32 * @param len - length of the key in bytes 33 * @param seed - seed which is used to calculate hash 34 * @return 32 bits hash 35 */ GetHash32WithSeed(const uint8_t * key,size_t len,uint32_t seed)36 static uint32_t GetHash32WithSeed(const uint8_t *key, size_t len, uint32_t seed) 37 { 38 return HashImpl::GetHash32WithSeedImpl(key, len, seed); 39 } 40 /** 41 * \brief Create 32 bits Hash from \param key. 42 * @param key - a key which should be hashed 43 * @param len - length of the key in bytes 44 * @return 32 bits hash 45 */ GetHash32(const uint8_t * key,size_t len)46 static uint32_t GetHash32(const uint8_t *key, size_t len) 47 { 48 return HashImpl::GetHash32Impl(key, len); 49 } 50 /** 51 * \brief Create 32 bits Hash from MUTF8 \param string. 52 * @param string - a pointer to the MUTF8 string 53 * @return 32 bits hash 54 */ GetHash32String(const uint8_t * mutf8_string)55 static uint32_t GetHash32String(const uint8_t *mutf8_string) 56 { 57 return HashImpl::GetHash32StringImpl(mutf8_string); 58 } 59 /** 60 * \brief Create 32 bits Hash from MUTF8 \param string. 61 * @param string - a pointer to the MUTF8 string 62 * @param seed - seed which is used to calculate hash 63 * @return 32 bits hash 64 */ GetHash32StringWithSeed(const uint8_t * mutf8_string,uint32_t seed)65 static uint32_t GetHash32StringWithSeed(const uint8_t *mutf8_string, uint32_t seed) 66 { 67 return HashImpl::GetHash32StringWithSeedImpl(mutf8_string, seed); 68 } 69 }; 70 71 } // namespace panda 72 73 #endif // LIBPANDABASE_UTILS_HASH_BASE_H_ 74