• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     size_t pos = 0;
52     size_t found = value.find('/');
53     while (found != std::string::npos) {
54         if (pos != found) {
55             res += GetAnonyString(value.substr(pos, found - pos));
56         }
57         res += '/';
58         pos = found + 1;
59         found = value.find('/', pos);
60     }
61     //处理文件名
62     if (pos < value.length()) {
63         found = value.find('.', pos);
64         if (found != std::string::npos) {
65             res += GetAnonyString(value.substr(pos, found - pos));
66             res += value.substr(found);
67         } else {
68             res += GetAnonyString(value.substr(pos));
69         }
70     }
71     return res;
72 }
73 }