diff --git a/interfaces/innerkits/appverify/include/interfaces/hap_verify.h b/interfaces/innerkits/appverify/include/interfaces/hap_verify.h index c7752913832ef0a262b86e09a5976aea1f22b3e1..69ae3339ebfa67a38f126df330c453cd02dbabcb 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 0000000000000000000000000000000000000000..81866410a48c5f3de2b9f11188d530a821e1f60c --- /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 9d366ff540cf7459e3a14675855efa698130bf23..b0d54c55e4fe913859ac1c0ddb405b430084fbd9 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 0e1a27689d56ad35664d1e1e30394dd6c8136299..170d69fe7a1604a27ce746efd26d21951f37933c 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); +} }