From 71ef76a863e2f5b9c45c54e106d76eccd4cddfc9 Mon Sep 17 00:00:00 2001 From: tangfan Date: Thu, 11 Aug 2022 10:00:41 +0800 Subject: [PATCH 1/2] add sha256 in utils func Signed-off-by: tangfan --- utils/include/dh_utils_tool.h | 2 ++ utils/src/dh_utils_tool.cpp | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index 38a2bf27..a81b74f0 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -38,6 +38,8 @@ DeviceInfo GetLocalDeviceInfo(); /* Convert uuid to deviceId by sha256 encode */ std::string GetDeviceIdByUUID(const std::string &uuid); + +std::string Sha256(const std::string& string); } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index a800a0df..55b8c922 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -82,24 +82,30 @@ std::string GetUUIDBySoftBus(const std::string &networkId) std::string GetDeviceIdByUUID(const std::string &uuid) { - unsigned char hash[SHA256_DIGEST_LENGTH * 2 + 1] = {0}; + return Sha256(uuid); +} + +std::string Sha256(const std::string& in) +{ + unsigned char out[SHA256_DIGEST_LENGTH * 2 + 1] = {0}; SHA256_CTX ctx; SHA256_Init(&ctx); - SHA256_Update(&ctx, uuid.data(), uuid.size()); - SHA256_Final(&hash[SHA256_DIGEST_LENGTH], &ctx); + SHA256_Update(&ctx, in.data(), in.size()); + SHA256_Final(&out[SHA256_DIGEST_LENGTH], &ctx); // here we translate sha256 hash to hexadecimal. each 8-bit char will be presented by two characters([0-9a-f]) constexpr int32_t WIDTH = 4; constexpr unsigned char MASK = 0x0F; const char* hexCode = "0123456789abcdef"; constexpr int32_t DOUBLE_TIMES = 2; for (int32_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) { - unsigned char value = hash[SHA256_DIGEST_LENGTH + i]; + + unsigned char value = out[SHA256_DIGEST_LENGTH + i]; // uint8_t is 2 digits in hexadecimal. - hash[i * DOUBLE_TIMES] = hexCode[(value >> WIDTH) & MASK]; - hash[i * DOUBLE_TIMES + 1] = hexCode[value & MASK]; + out[i * DOUBLE_TIMES] = hexCode[(value >> WIDTH) & MASK]; + out[i * DOUBLE_TIMES + 1] = hexCode[value & MASK]; } - hash[SHA256_DIGEST_LENGTH * DOUBLE_TIMES] = 0; - return reinterpret_cast(hash); + out[SHA256_DIGEST_LENGTH * DOUBLE_TIMES] = 0; + return reinterpret_cast(out); } DeviceInfo GetLocalDeviceInfo() -- Gitee From 686abc7ac33618ac0aa1ed7a62d4f247651b7583 Mon Sep 17 00:00:00 2001 From: tangfan Date: Thu, 11 Aug 2022 10:53:05 +0800 Subject: [PATCH 2/2] add sha256 in utils func Signed-off-by: tangfan --- utils/src/dh_utils_tool.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index 55b8c922..0fb238b3 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -98,7 +98,6 @@ std::string Sha256(const std::string& in) const char* hexCode = "0123456789abcdef"; constexpr int32_t DOUBLE_TIMES = 2; for (int32_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) { - unsigned char value = out[SHA256_DIGEST_LENGTH + i]; // uint8_t is 2 digits in hexadecimal. out[i * DOUBLE_TIMES] = hexCode[(value >> WIDTH) & MASK]; -- Gitee