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 "sharing_log.h"
23 #include "utils/crypto.h"
24
25 namespace OHOS {
26 namespace Sharing {
27 const int BYTE_HEX_LEN = 2;
ByteToHexStr(const std::vector<uint8_t> & data,uint32_t pos,uint32_t len,bool isNeed0x)28 std::string ByteToHexStr(const std::vector<uint8_t> &data, uint32_t pos, uint32_t len, bool isNeed0x)
29 {
30 std::stringstream ss;
31 if (isNeed0x) {
32 ss << "0x" << std::hex << std::setfill('0');
33 } else {
34 ss << std::hex << std::setfill('0');
35 }
36 for (uint32_t i = pos; (i < (pos + len)) && (i < data.size()); i++) {
37 ss << std::setw(BYTE_HEX_LEN) << static_cast<int>(data[i]);
38 }
39 return ss.str();
40 }
41
GetUdidHash(std::string & udid)42 std::string GetUdidHash(std::string &udid)
43 {
44 if (udid.empty()) {
45 return "";
46 }
47 uint32_t udidLen = udid.size();
48 std::vector<uint8_t> hashIdResult = GenerateSha256HashId(reinterpret_cast<const uint8_t *>(udid.c_str()), udidLen);
49 if (hashIdResult.empty()) {
50 return "";
51 }
52 std::string ret = ByteToHexStr(hashIdResult, 0, hashIdResult.size(), false);
53 return ret;
54 }
55
GetAddressHash(const std::string & address)56 std::string GetAddressHash(const std::string &address)
57 {
58 std::string addressStr = std::string(address);
59 return GetUdidHash(addressStr);
60 }
61 } // namespace Sharing
62 } // namespace OHOS