1 /* 2 * Copyright (c) 2022 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 "securec.h" 19 20 namespace OHOS { 21 namespace DistributedHardware { GetAnonyString(const std::string & value)22std::string GetAnonyString(const std::string &value) 23 { 24 constexpr size_t INT32_SHORT_ID_LENGTH = 20; 25 constexpr size_t INT32_PLAINTEXT_LENGTH = 4; 26 constexpr size_t INT32_MIN_ID_LENGTH = 3; 27 std::string res; 28 std::string tmpStr("******"); 29 size_t strLen = value.length(); 30 if (strLen < INT32_MIN_ID_LENGTH) { 31 return tmpStr; 32 } 33 34 if (strLen <= INT32_SHORT_ID_LENGTH) { 35 res += value[0]; 36 res += tmpStr; 37 res += value[strLen - 1]; 38 } else { 39 res.append(value, 0, INT32_PLAINTEXT_LENGTH); 40 res += tmpStr; 41 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH); 42 } 43 44 return res; 45 } 46 } // namespace DistributedHardware 47 } // namespace OHOS 48