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 #define LOG_TAG "KvStoreUtils"
16 
17 #include "kvstore_utils.h"
18 #include <limits>
19 
20 namespace OHOS {
21 namespace DistributedKv {
22 constexpr int32_t HEAD_SIZE = 3;
23 constexpr int32_t END_SIZE = 3;
24 constexpr int32_t MIN_SIZE = HEAD_SIZE + END_SIZE + 3;
25 constexpr const char *REPLACE_CHAIN = "***";
26 constexpr const char *DEFAULT_ANONYMOUS = "******";
27 std::atomic<uint64_t> KvStoreUtils::sequenceId_{ 0 };
ToBeAnonymous(const std::string & name)28 std::string KvStoreUtils::ToBeAnonymous(const std::string &name)
29 {
30     if (name.length() <= HEAD_SIZE) {
31         return DEFAULT_ANONYMOUS;
32     }
33 
34     if (name.length() < MIN_SIZE) {
35         return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN);
36     }
37 
38     return (name.substr(0, HEAD_SIZE) + REPLACE_CHAIN + name.substr(name.length() - END_SIZE, END_SIZE));
39 }
40 
GenerateSequenceId()41 uint64_t KvStoreUtils::GenerateSequenceId()
42 {
43     uint64_t seqId = ++sequenceId_;
44     if (seqId == std::numeric_limits<uint64_t>::max()) {
45         return ++sequenceId_;
46     }
47     return seqId;
48 }
49 } // namespace DistributedKv
50 } // namespace OHOS
51