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 <iomanip>
20 #include <random>
21 #include <string>
22 #include <sstream>
23
24 #include "securec.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 constexpr int32_t WORD_WIDTH_8 = 8;
29 constexpr int32_t WORD_WIDTH_4 = 4;
30
GetRandomID()31 std::string GetRandomID()
32 {
33 static std::random_device rd;
34 static std::uniform_int_distribution<uint64_t> dist(0ULL, 0xFFFFFFFFFFFFFFFFULL);
35 uint64_t ab = dist(rd);
36 uint64_t cd = dist(rd);
37 uint32_t a, b, c, d;
38 std::stringstream ss;
39 ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL;
40 cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL;
41 a = (ab >> 32U);
42 b = (ab & 0xFFFFFFFFU);
43 c = (cd >> 32U);
44 d = (cd & 0xFFFFFFFFU);
45 ss << std::hex << std::nouppercase << std::setfill('0');
46 ss << std::setw(WORD_WIDTH_8) << (a);
47 ss << std::setw(WORD_WIDTH_4) << (b >> 16U);
48 ss << std::setw(WORD_WIDTH_4) << (b & 0xFFFFU);
49 ss << std::setw(WORD_WIDTH_4) << (c >> 16U);
50 ss << std::setw(WORD_WIDTH_4) << (c & 0xFFFFU);
51 ss << std::setw(WORD_WIDTH_8) << d;
52
53 return ss.str();
54 }
55
GetAnonyString(const std::string & value)56 std::string GetAnonyString(const std::string &value)
57 {
58 constexpr size_t INT32_SHORT_ID_LENGTH = 20;
59 constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
60 constexpr size_t INT32_MIN_ID_LENGTH = 3;
61 std::string res;
62 std::string tmpStr("******");
63 size_t strLen = value.length();
64 if (strLen < INT32_MIN_ID_LENGTH) {
65 return tmpStr;
66 }
67
68 if (strLen <= INT32_SHORT_ID_LENGTH) {
69 res += value[0];
70 res += tmpStr;
71 res += value[strLen - 1];
72 } else {
73 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
74 res += tmpStr;
75 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
76 }
77
78 return res;
79 }
80
GetAnonyInt32(const int32_t value)81 std::string GetAnonyInt32(const int32_t value)
82 {
83 constexpr int32_t INT32_STRING_LENGTH = 40;
84 char tempBuffer[INT32_STRING_LENGTH] = "";
85 int32_t secRet = sprintf_s(tempBuffer, INT32_STRING_LENGTH, "%d", value);
86 if (secRet <= 0) {
87 std::string nullString("");
88 return nullString;
89 }
90 size_t length = strlen(tempBuffer);
91 for (size_t i = 1; i <= length - 1; i++) {
92 tempBuffer[i] = '*';
93 }
94 if (length == 0x01) {
95 tempBuffer[0] = '*';
96 }
97
98 std::string tempString(tempBuffer);
99 return tempString;
100 }
101 } // namespace DistributedHardware
102 } // namespace OHOS
103