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