1 /*
2  * Copyright (c) 2024 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 "b_anony/b_anony.h"
17 
18 #include "securec.h"
19 
20 namespace OHOS::FileManagement::Backup {
21 using namespace std;
22 
GetAnonyString(const std::string & value)23 std::string GetAnonyString(const std::string &value)
24 {
25     const size_t shortPlaintextLength = 20; // 字符串长度小于20,只明文1字节
26     const size_t plaintextLength = 4; // 字符串长度大于20,明文4字节
27     const size_t anonyLength = 3; // 字符串长度小于3, 则完全匿名
28     std::string result;
29     std::string tmpStr("******");
30     size_t strLen = value.length();
31     if (strLen < anonyLength) {
32         return tmpStr;
33     }
34 
35     if (strLen <= shortPlaintextLength) {
36         result += value[0];
37         result += tmpStr;
38         result += value[strLen - 1];
39     } else {
40         result += value.substr(0, plaintextLength);
41         result += tmpStr;
42         result += value.substr(strLen - plaintextLength, plaintextLength);
43     }
44 
45     return result;
46 }
47 
GetAnonyPath(const std::string & value)48 std::string GetAnonyPath(const std::string &value)
49 {
50     std::string res;
51     std::string sub;
52     size_t found = value.find_last_of('/');
53     if (found == std::string::npos) {
54         sub = value;
55     } else {
56         sub = value.substr(found + 1);
57         res = value.substr(0, found + 1);
58     }
59     res += GetAnonyString(sub);
60 
61     return res;
62 }
63 }