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