1 /*
2 * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 #include "wfd_utils.h"
17 #include <iomanip>
18 #include <iostream>
19 #include <mutex>
20 #include <sstream>
21 #include <vector>
22 #include <openssl/sha.h>
23 #include "sharing_log.h"
24 #include "utils/crypto.h"
25
26 namespace OHOS {
27 namespace Sharing {
28 constexpr int32_t HEX_TO_UINT8 = 2;
29 constexpr int WIDTH = 4;
30 constexpr unsigned char MASK = 0x0F;
31 const int BYTE_HEX_LEN = 2;
ByteToHexStr(const std::vector<uint8_t> & data,uint32_t pos,uint32_t len,bool isNeed0x)32 std::string ByteToHexStr(const std::vector<uint8_t> &data, uint32_t pos, uint32_t len, bool isNeed0x)
33 {
34 std::stringstream ss;
35 if (isNeed0x) {
36 ss << "0x" << std::hex << std::setfill('0');
37 } else {
38 ss << std::hex << std::setfill('0');
39 }
40 for (uint32_t i = pos; (i < (pos + len)) && (i < data.size()); i++) {
41 ss << std::setw(BYTE_HEX_LEN) << static_cast<int>(data[i]);
42 }
43 return ss.str();
44 }
45
GetUdidHash(std::string & udid)46 std::string GetUdidHash(std::string &udid)
47 {
48 if (udid.empty()) {
49 return "";
50 }
51 uint32_t udidLen = udid.size();
52 std::vector<uint8_t> hashIdResult = GenerateSha256HashId(reinterpret_cast<const uint8_t *>(udid.c_str()), udidLen);
53 if (hashIdResult.empty()) {
54 return "";
55 }
56 std::string ret = ByteToHexStr(hashIdResult, 0, hashIdResult.size(), false);
57 return ret;
58 }
59
GetAddressHash(const std::string & address)60 std::string GetAddressHash(const std::string &address)
61 {
62 std::string addressStr = std::string(address);
63 return GetUdidHash(addressStr);
64 }
65
Sha256(const std::string & text,bool isUpper)66 std::string Sha256(const std::string &text, bool isUpper)
67 {
68 return Sha256(text.data(), text.size(), isUpper);
69 }
70
Sha256(const void * data,size_t size,bool isUpper)71 std::string Sha256(const void *data, size_t size, bool isUpper)
72 {
73 unsigned char hash[SHA256_DIGEST_LENGTH * HEX_TO_UINT8 + 1] = "";
74 SHA256_CTX ctx;
75 SHA256_Init(&ctx);
76 SHA256_Update(&ctx, data, size);
77 SHA256_Final(&hash[SHA256_DIGEST_LENGTH], &ctx);
78 const char* hexCode = isUpper ? "0123456789ABCDEF" : "0123456789abcdef";
79 for (int32_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
80 unsigned char value = hash[SHA256_DIGEST_LENGTH + i];
81 hash[i * HEX_TO_UINT8] = hexCode[(value >> WIDTH) & MASK];
82 hash[i * HEX_TO_UINT8 + 1] = hexCode[value & MASK];
83 }
84 hash[SHA256_DIGEST_LENGTH * HEX_TO_UINT8] = 0;
85 std::stringstream ss;
86 ss << hash;
87 return ss.str();
88 }
89 } // namespace Sharing
90 } // namespace OHOS