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 "dm_anonymous.h" 17 #include "dm_log.h" 18 19 namespace OHOS { 20 namespace DistributedHardware { GetAnonyString(const std::string & value)21std::string GetAnonyString(const std::string &value) 22 { 23 const int32_t INT32_SHORT_ID_LENGTH = 20; 24 const int32_t INT32_PLAINTEXT_LENGTH = 4; 25 const int32_t INT32_MIN_ID_LENGTH = 3; 26 27 std::string tmpStr("******"); 28 size_t strLen = value.length(); 29 if (strLen < INT32_MIN_ID_LENGTH) { 30 return tmpStr; 31 } 32 33 std::string res; 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 GetAnonyInt32(const int32_t value)47std::string GetAnonyInt32(const int32_t value) 48 { 49 std::string tempString = std::to_string(value); 50 size_t length = tempString.length(); 51 if (length == 0x01) { 52 tempString[0] = '*'; 53 return tempString; 54 } 55 for (size_t i = 1; i < length - 1; i++) { 56 tempString[i] = '*'; 57 } 58 return tempString; 59 } 60 IsNumberString(const std::string & inputString)61bool IsNumberString(const std::string &inputString) 62 { 63 LOGI("IsNumberString for DeviceManagerNapi"); 64 if (inputString.length() == 0) { 65 LOGE("inputString is Null"); 66 return false; 67 } 68 const int32_t MIN_ASCLL_NUM = 48; 69 const int32_t MAX_ASCLL_NUM = 57; 70 for (int i = 0; i < inputString.length(); i++) { 71 int num = (int)inputString[i]; 72 if (num >= MIN_ASCLL_NUM && num <= MAX_ASCLL_NUM) { 73 continue; 74 } else { 75 return false; 76 } 77 } 78 return true; 79 } 80 } // namespace DistributedHardware 81 } // namespace OHOS 82