1 /*
2 * Copyright (c) 2021-2024 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 #include "anonymous_string.h"
17
18 #include <cstddef>
19 #include <string>
20
21 #include "constants.h"
22 #include "dh_utils_tool.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "securec.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30 constexpr size_t INT32_SHORT_ID_LENGTH = 20;
31 constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
32 constexpr size_t INT32_MIN_ID_LENGTH = 3;
33 constexpr int32_t INT32_STRING_LENGTH = 40;
34 }
GetAnonyString(const std::string & value)35 std::string GetAnonyString(const std::string &value)
36 {
37 if (!IsMessageLengthValid(value)) {
38 return "";
39 }
40 std::string res;
41 std::string tmpStr("******");
42 size_t strLen = value.length();
43 if (strLen < INT32_MIN_ID_LENGTH) {
44 return tmpStr;
45 }
46
47 if (strLen <= INT32_SHORT_ID_LENGTH) {
48 res += value[0];
49 res += tmpStr;
50 res += value[strLen - 1];
51 } else {
52 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
53 res += tmpStr;
54 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
55 }
56
57 return res;
58 }
59
GetAnonyInt32(const int32_t value)60 std::string GetAnonyInt32(const int32_t value)
61 {
62 char tempBuffer[INT32_STRING_LENGTH] = "";
63 int32_t secRet = sprintf_s(tempBuffer, INT32_STRING_LENGTH, "%d", value);
64 if (secRet <= 0) {
65 std::string nullString("");
66 return nullString;
67 }
68 size_t length = strlen(tempBuffer);
69 if (length < 1) {
70 std::string nullString("");
71 return nullString;
72 }
73 for (size_t i = 1; i <= length - 1; i++) {
74 tempBuffer[i] = '*';
75 }
76 if (length == 0x01) {
77 tempBuffer[0] = '*';
78 }
79
80 std::string tempString(tempBuffer);
81 return tempString;
82 }
83 } // namespace DistributedHardware
84 } // namespace OHOS
85