1190978c3Sopenharmony_ci/*
2190978c3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3190978c3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4190978c3Sopenharmony_ci * you may not use this file except in compliance with the License.
5190978c3Sopenharmony_ci * You may obtain a copy of the License at
6190978c3Sopenharmony_ci *
7190978c3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8190978c3Sopenharmony_ci *
9190978c3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10190978c3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11190978c3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12190978c3Sopenharmony_ci * See the License for the specific language governing permissions and
13190978c3Sopenharmony_ci * limitations under the License.
14190978c3Sopenharmony_ci */
15190978c3Sopenharmony_ci
16190978c3Sopenharmony_ci#ifndef ANONYMOUS_UTILS_H
17190978c3Sopenharmony_ci#define ANONYMOUS_UTILS_H
18190978c3Sopenharmony_ci
19190978c3Sopenharmony_ci#include <chrono>
20190978c3Sopenharmony_ci#include <cstdio>
21190978c3Sopenharmony_ci#include <stdlib.h>
22190978c3Sopenharmony_ci#include <string>
23190978c3Sopenharmony_ci
24190978c3Sopenharmony_cinamespace OHOS {
25190978c3Sopenharmony_cinamespace UpdateEngine {
26190978c3Sopenharmony_cistatic const int32_t ENCRYPT_LENGTH = 4; // 需要替换*的长度
27190978c3Sopenharmony_cistatic const int32_t ENCRYPT_TOTAL_LENGTH = 8; // 敏感数据匿名化后最长长度
28190978c3Sopenharmony_cistatic const std::string ENCRYPT_STR = "****";
29190978c3Sopenharmony_ci
30190978c3Sopenharmony_ciclass AnonymousUtils {
31190978c3Sopenharmony_cipublic:
32190978c3Sopenharmony_ci    static std::string AnonymousUrl(const std::string &url)
33190978c3Sopenharmony_ci    {
34190978c3Sopenharmony_ci        std::string encryptUrl = url;
35190978c3Sopenharmony_ci        std::string httpsPrefix = "https://";
36190978c3Sopenharmony_ci        std::string httpPrefix = "http://";
37190978c3Sopenharmony_ci
38190978c3Sopenharmony_ci        // 从https:// 或者 http:// 开始后面4位替换为 xxxx
39190978c3Sopenharmony_ci        if (encryptUrl.compare(0, httpsPrefix.size(), httpsPrefix) == 0) {
40190978c3Sopenharmony_ci            encryptUrl.replace(httpsPrefix.size(), ENCRYPT_LENGTH, ENCRYPT_STR);
41190978c3Sopenharmony_ci            return encryptUrl;
42190978c3Sopenharmony_ci        }
43190978c3Sopenharmony_ci        if (encryptUrl.compare(0, httpPrefix.size(), httpPrefix) == 0) {
44190978c3Sopenharmony_ci            encryptUrl.replace(httpPrefix.size(), ENCRYPT_LENGTH, ENCRYPT_STR);
45190978c3Sopenharmony_ci            return encryptUrl;
46190978c3Sopenharmony_ci        }
47190978c3Sopenharmony_ci        return encryptUrl;
48190978c3Sopenharmony_ci    }
49190978c3Sopenharmony_ci
50190978c3Sopenharmony_ci    static std::string AnonymousString(std::string inputStr)
51190978c3Sopenharmony_ci    {
52190978c3Sopenharmony_ci        if (inputStr.empty()) {
53190978c3Sopenharmony_ci            return inputStr;
54190978c3Sopenharmony_ci        }
55190978c3Sopenharmony_ci        std::string result;
56190978c3Sopenharmony_ci        size_t length = inputStr.length();
57190978c3Sopenharmony_ci        if (length >= ENCRYPT_TOTAL_LENGTH) {
58190978c3Sopenharmony_ci            std::string sequence = inputStr.substr(0, ENCRYPT_LENGTH);
59190978c3Sopenharmony_ci            result = sequence + ENCRYPT_STR;
60190978c3Sopenharmony_ci        } else if (length > ENCRYPT_LENGTH) {
61190978c3Sopenharmony_ci            std::string sequence = inputStr.substr(0, length - ENCRYPT_LENGTH);
62190978c3Sopenharmony_ci            result = sequence + ENCRYPT_STR;
63190978c3Sopenharmony_ci        } else {
64190978c3Sopenharmony_ci            result = ENCRYPT_STR;
65190978c3Sopenharmony_ci        }
66190978c3Sopenharmony_ci        return result;
67190978c3Sopenharmony_ci    }
68190978c3Sopenharmony_ci};
69190978c3Sopenharmony_ci} // namespace UpdateEngine
70190978c3Sopenharmony_ci} // namespace OHOS
71190978c3Sopenharmony_ci#endif // ANONYMOUS_UTILS_H