From e8a3aa80a2efabcdb3763ea05c3f7bbaf6d5c460 Mon Sep 17 00:00:00 2001 From: zhaogan Date: Mon, 16 Jun 2025 15:08:18 +0800 Subject: [PATCH] hash Signed-off-by: zhaogan --- .../appverify/include/interfaces/hap_verify.h | 3 +- .../appverify/include/util/string_hash.h | 64 +++++++++++++++++++ .../appverify/src/interfaces/hap_verify.cpp | 7 +- .../test/unittest/src/hap_verify_test.cpp | 22 +++++++ 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 interfaces/innerkits/appverify/include/util/string_hash.h diff --git a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h index c775291..69ae333 100644 --- a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h +++ b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2025 Huawei Device Co., Ltd. * 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 @@ -33,6 +33,7 @@ DLL_EXPORT int32_t ParseHapSignatureInfo(const std::string& filePath, SignatureI extern "C" DLL_EXPORT int32_t ParseBundleNameAndAppIdentifier(const int32_t fileFd, std::string &bundleName, std::string &appIdentifier); DLL_EXPORT void SetDevMode(DevMode devMode); +DLL_EXPORT std::string GenerateUuidByKey(const std::string &key); } // namespace Verify } // namespace Security } // namespace OHOS diff --git a/interfaces/innerkits/appverify/include/util/string_hash.h b/interfaces/innerkits/appverify/include/util/string_hash.h new file mode 100644 index 0000000..8186641 --- /dev/null +++ b/interfaces/innerkits/appverify/include/util/string_hash.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * 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. + */ +#ifndef HAP_UTIL_STRING_HASH_H +#define HAP_UTIL_STRING_HASH_H + +#include +#include +#include +#include +#include + +namespace OHOS { +namespace Security { +namespace Verify { +// for separator +constexpr char UUID_SEPARATOR = '-'; +const std::vector SEPARATOR_POSITIONS { 8, 13, 18, 23}; +const size_t UUID_ORIGIN_SIZE = 32; +const uint8_t BIT_TWO = 2; +class StringHash { +public: + // Generate SHA-256 hash of the input string + static std::string GenerateUuidByKey(const std::string &input) + { + // SHA256 produces 32-byte hash + unsigned char hash[SHA256_DIGEST_LENGTH]; + + // Compute SHA256 + SHA256_CTX sha256; + SHA256_Init(&sha256); // Initialize context + SHA256_Update(&sha256, input.c_str(), input.size()); // Feed data to hash + SHA256_Final(hash, &sha256); // Get final hash + + // Convert binary hash to hexadecimal string + std::stringstream ss; + for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { + ss << std::hex << std::setw(BIT_TWO) << std::setfill('0') << (int)hash[i]; + } + std::string hashString = ss.str(); + // Format the hash string to match UUID format + hashString = hashString.substr(0, UUID_ORIGIN_SIZE); + for (int32_t index : SEPARATOR_POSITIONS) { + hashString.insert(index, 1, UUID_SEPARATOR); + } + + return hashString; + } +}; +} // namespace Verify +} // namespace Security +} // namespace OHOS +#endif // HAP_UTIL_STRING_HASH_H diff --git a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp index 9d366ff..b0d54c5 100644 --- a/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp +++ b/interfaces/innerkits/appverify/src/interfaces/hap_verify.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2025 Huawei Device Co., Ltd. * 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 @@ -25,6 +25,7 @@ #include "init/trusted_ticket_manager.h" #include "provision/provision_verify.h" #include "verify/hap_verify_v2.h" +#include "util/string_hash.h" namespace OHOS { namespace Security { @@ -134,6 +135,10 @@ int32_t ParseBundleNameAndAppIdentifier(const int32_t fileFd, std::string &bundl return VERIFY_SUCCESS; } +std::string GenerateUuidByKey(const std::string &key) +{ + return StringHash::GenerateUuidByKey(key); +} } // namespace Verify } // namespace Security } // namespace OHOS diff --git a/interfaces/innerkits/appverify/test/unittest/src/hap_verify_test.cpp b/interfaces/innerkits/appverify/test/unittest/src/hap_verify_test.cpp index 0e1a276..170d69f 100644 --- a/interfaces/innerkits/appverify/test/unittest/src/hap_verify_test.cpp +++ b/interfaces/innerkits/appverify/test/unittest/src/hap_verify_test.cpp @@ -304,4 +304,26 @@ HWTEST_F(HapVerifyTest, HapVerify006, TestSize.Level1) EXPECT_FALSE(ParseBundleNameAndAppIdentifier(fd, bundleName, appIdentifier) == VERIFY_SOURCE_INIT_FAIL); close(fd); } + +/** + * @tc.name: HapVerifyTest.StringHash001 + * @tc.desc: The static function will return hash string; + * @tc.type: FUNC + */ +HWTEST_F(HapVerifyTest, StringHash001, TestSize.Level1) +{ + std::string message1 = "1315398076000C152942D948D3EB835324C80534674129456FF6F951105E0E711B1A356E39269"; + std::string message2 = message1; + std::string message3 = "1303837768000C152942D948D3EB835324C80534674129456FF6F951105E0E711B1A356E39269"; + + std::string uuid1 = GenerateUuidByKey(message1); + EXPECT_EQ(uuid1.size(), 36); + std::string uuid2 = GenerateUuidByKey(message2); + EXPECT_EQ(uuid2.size(), 36); + std::string uuid3 = GenerateUuidByKey(message3); + EXPECT_EQ(uuid3.size(), 36); + + EXPECT_EQ(uuid1, uuid2); + EXPECT_NE(uuid1, uuid3); +} } -- Gitee