1794c9f46Sopenharmony_ci/*
2794c9f46Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3794c9f46Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4794c9f46Sopenharmony_ci * you may not use this file except in compliance with the License.
5794c9f46Sopenharmony_ci * You may obtain a copy of the License at
6794c9f46Sopenharmony_ci *
7794c9f46Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8794c9f46Sopenharmony_ci *
9794c9f46Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10794c9f46Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11794c9f46Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12794c9f46Sopenharmony_ci * See the License for the specific language governing permissions and
13794c9f46Sopenharmony_ci * limitations under the License.
14794c9f46Sopenharmony_ci */
15794c9f46Sopenharmony_ci
16794c9f46Sopenharmony_ci#include "anonymous_string.h"
17794c9f46Sopenharmony_ci
18794c9f46Sopenharmony_ci#include <cstddef>
19794c9f46Sopenharmony_ci#include <string>
20794c9f46Sopenharmony_ci
21794c9f46Sopenharmony_ci#include "constants.h"
22794c9f46Sopenharmony_ci#include "dh_utils_tool.h"
23794c9f46Sopenharmony_ci#include "distributed_hardware_errno.h"
24794c9f46Sopenharmony_ci#include "distributed_hardware_log.h"
25794c9f46Sopenharmony_ci#include "securec.h"
26794c9f46Sopenharmony_ci
27794c9f46Sopenharmony_cinamespace OHOS {
28794c9f46Sopenharmony_cinamespace DistributedHardware {
29794c9f46Sopenharmony_cinamespace {
30794c9f46Sopenharmony_ci    constexpr size_t INT32_SHORT_ID_LENGTH = 20;
31794c9f46Sopenharmony_ci    constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
32794c9f46Sopenharmony_ci    constexpr size_t INT32_MIN_ID_LENGTH = 3;
33794c9f46Sopenharmony_ci    constexpr int32_t INT32_STRING_LENGTH = 40;
34794c9f46Sopenharmony_ci}
35794c9f46Sopenharmony_cistd::string GetAnonyString(const std::string &value)
36794c9f46Sopenharmony_ci{
37794c9f46Sopenharmony_ci    if (!IsMessageLengthValid(value)) {
38794c9f46Sopenharmony_ci        return "";
39794c9f46Sopenharmony_ci    }
40794c9f46Sopenharmony_ci    std::string res;
41794c9f46Sopenharmony_ci    std::string tmpStr("******");
42794c9f46Sopenharmony_ci    size_t strLen = value.length();
43794c9f46Sopenharmony_ci    if (strLen < INT32_MIN_ID_LENGTH) {
44794c9f46Sopenharmony_ci        return tmpStr;
45794c9f46Sopenharmony_ci    }
46794c9f46Sopenharmony_ci
47794c9f46Sopenharmony_ci    if (strLen <= INT32_SHORT_ID_LENGTH) {
48794c9f46Sopenharmony_ci        res += value[0];
49794c9f46Sopenharmony_ci        res += tmpStr;
50794c9f46Sopenharmony_ci        res += value[strLen - 1];
51794c9f46Sopenharmony_ci    } else {
52794c9f46Sopenharmony_ci        res.append(value, 0, INT32_PLAINTEXT_LENGTH);
53794c9f46Sopenharmony_ci        res += tmpStr;
54794c9f46Sopenharmony_ci        res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
55794c9f46Sopenharmony_ci    }
56794c9f46Sopenharmony_ci
57794c9f46Sopenharmony_ci    return res;
58794c9f46Sopenharmony_ci}
59794c9f46Sopenharmony_ci
60794c9f46Sopenharmony_cistd::string GetAnonyInt32(const int32_t value)
61794c9f46Sopenharmony_ci{
62794c9f46Sopenharmony_ci    char tempBuffer[INT32_STRING_LENGTH] = "";
63794c9f46Sopenharmony_ci    int32_t secRet = sprintf_s(tempBuffer, INT32_STRING_LENGTH, "%d", value);
64794c9f46Sopenharmony_ci    if (secRet <= 0) {
65794c9f46Sopenharmony_ci        std::string nullString("");
66794c9f46Sopenharmony_ci        return nullString;
67794c9f46Sopenharmony_ci    }
68794c9f46Sopenharmony_ci    size_t length = strlen(tempBuffer);
69794c9f46Sopenharmony_ci    if (length < 1) {
70794c9f46Sopenharmony_ci        std::string nullString("");
71794c9f46Sopenharmony_ci        return nullString;
72794c9f46Sopenharmony_ci    }
73794c9f46Sopenharmony_ci    for (size_t i = 1; i <= length - 1; i++) {
74794c9f46Sopenharmony_ci        tempBuffer[i] = '*';
75794c9f46Sopenharmony_ci    }
76794c9f46Sopenharmony_ci    if (length == 0x01) {
77794c9f46Sopenharmony_ci        tempBuffer[0] = '*';
78794c9f46Sopenharmony_ci    }
79794c9f46Sopenharmony_ci
80794c9f46Sopenharmony_ci    std::string tempString(tempBuffer);
81794c9f46Sopenharmony_ci    return tempString;
82794c9f46Sopenharmony_ci}
83794c9f46Sopenharmony_ci} // namespace DistributedHardware
84794c9f46Sopenharmony_ci} // namespace OHOS
85