• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device 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 #ifndef SOFTBUS_SESSION_UTILS_H
17 #define SOFTBUS_SESSION_UTILS_H
18 
19 #include <string>
20 #include "json/json.h"
21 #include "migrate_avsession_constant.h"
22 
23 namespace OHOS::AVSession {
24 class SoftbusSessionUtils {
25 public:
AnonymizeDeviceId(const std::string & deviceId)26     static std::string AnonymizeDeviceId(const std::string &deviceId)
27     {
28         if (deviceId == "") {
29             return "";
30         }
31         if (deviceId.length() <= DEVICE_ANONYMIZE_LENGTH) {
32             return std::string(DEVICE_ANONYMIZE_ID);
33         } else {
34             unsigned long index =
35                 std::min<unsigned long>(deviceId.length() - DEVICE_ANONYMIZE_LENGTH, DEVICE_ANONYMIZE_LENGTH);
36             std::string prefix = deviceId.substr(0, index);
37             std::string suffix = deviceId.substr(deviceId.length() - index, deviceId.length());
38             return prefix + std::string(DEVICE_ANONYMIZE_ID) + suffix;
39         }
40     }
41 
TransferJsonToStr(const Json::Value & jsonValue,std::string & jsonStr)42     static inline void TransferJsonToStr(const Json::Value& jsonValue, std::string& jsonStr)
43     {
44         Json::FastWriter writer;
45         jsonStr += writer.write(jsonValue);
46     }
47 
TransferStrToJson(const std::string & jsonStr,Json::Value & jsonValue)48     static inline bool TransferStrToJson(const std::string& jsonStr, Json::Value& jsonValue)
49     {
50         Json::Reader jsonReader;
51         if (!jsonReader.parse(jsonStr, jsonValue)) {
52             return false;
53         }
54         return true;
55     }
56 
GetEncryptAddr(const std::string & addr)57     static std::string GetEncryptAddr(const std::string& addr)
58     {
59         if (addr.empty() || addr.length() != addressStrLen) {
60             return std::string("");
61         }
62         std::string tmp = "**:**:**:**:**:**";
63         std::string out = addr;
64         for (int i = startPos; i <= endPos; i++) {
65             out[i] = tmp[i];
66         }
67         return out;
68     }
69 
AnonymizeMacAddressInSoftBusMsg(const std::string & input)70     static std::string AnonymizeMacAddressInSoftBusMsg(const std::string& input)
71     {
72         std::string output = input;
73         std::string macKey = "\"" + AUDIO_MAC_ADDRESS + "\":\"";
74         std::string endKey = "\"";
75         size_t startPos = 0;
76 
77         while ((startPos = output.find(macKey, startPos)) != std::string::npos) {
78             startPos += macKey.length();
79             size_t endPos = output.find(endKey, startPos);
80             if (endPos != std::string::npos) {
81                 std::string macAddress = output.substr(startPos, endPos - startPos);
82                 std::string encryptedMac = GetEncryptAddr(macAddress);
83                 output.replace(startPos, endPos - startPos, encryptedMac);
84             }
85         }
86         return output;
87     }
88 
89 private:
90     static constexpr const char *DEVICE_ANONYMIZE_ID = "******";
91     static constexpr const int DEVICE_ANONYMIZE_LENGTH = 6;
92 
93     static constexpr const int addressStrLen = 17;
94     static constexpr const int startPos = 6;
95     static constexpr const int endPos = 13;
96 };
97 } // namespace OHOS::AVSession
98 
99 #endif // SOFTBUS_SESSION_UTILS_H