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 "datashare_string_utils.h"
17 #include <cstdint>
18 #include <random>
19 
20 namespace OHOS {
21 namespace DataShare {
22 constexpr int32_t END_SIZE = 10;
23 constexpr int32_t ANONYMOUS_SIZE = 12;
24 constexpr const char *DEFAULT_ANONYMOUS = "******";
Anonymous(const std::string & name)25 std::string DataShareStringUtils::Anonymous(const std::string &name)
26 {
27     if (name.length() <= END_SIZE) {
28         return DEFAULT_ANONYMOUS;
29     }
30 
31     return (DEFAULT_ANONYMOUS + name.substr(name.length() - END_SIZE, END_SIZE));
32 }
33 
RemoveFromQuery(std::string & uri)34 void DataShareStringUtils::RemoveFromQuery(std::string &uri)
35 {
36     auto pos = uri.find_last_of('?');
37     if (pos == std::string::npos) {
38         return;
39     }
40     uri.resize(pos);
41 }
42 
Change(const std::string & name)43 std::string DataShareStringUtils::Change(const std::string &name)
44 {
45     if (name.length() <= ANONYMOUS_SIZE) {
46         return name;
47     }
48     return DEFAULT_ANONYMOUS + name.substr(ANONYMOUS_SIZE);
49 }
50 
GetRandomNumber(const int32_t min,const int32_t max)51 int32_t DataShareStringUtils::GetRandomNumber(const int32_t min, const int32_t max)
52 {
53     std::random_device rd;
54     std::mt19937 gen(rd());
55     std::uniform_int_distribution<> dis(min, max);
56     return dis(gen);
57 }
DataShareStringUtils()58 DataShareStringUtils::DataShareStringUtils() {}
~DataShareStringUtils()59 DataShareStringUtils::~DataShareStringUtils() {}
60 } // namespace DataShare
61 } // namespace OHOS