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