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 #include "metadata/secret_key_meta_data.h"
16 
17 #include "utils/constant.h"
18 namespace OHOS {
19 namespace DistributedData {
SecretKeyMetaData()20 SecretKeyMetaData::SecretKeyMetaData()
21 {
22 }
23 
~SecretKeyMetaData()24 SecretKeyMetaData::~SecretKeyMetaData()
25 {
26     sKey.assign(sKey.size(), 0);
27 }
28 
Marshal(json & node) const29 bool SecretKeyMetaData::Marshal(json &node) const
30 {
31     SetValue(node[GET_NAME(time)], time);
32     SetValue(node[GET_NAME(sKey)], sKey);
33     SetValue(node[GET_NAME(storeType)], storeType);
34     return true;
35 }
36 
Unmarshal(const json & node)37 bool SecretKeyMetaData::Unmarshal(const json &node)
38 {
39     GetValue(node, GET_NAME(time), time);
40     GetValue(node, GET_NAME(sKey), sKey);
41     GetValue(node, GET_NAME(storeType), storeType);
42     return true;
43 }
44 
GetKey(const std::initializer_list<std::string> & fields)45 std::string SecretKeyMetaData::GetKey(const std::initializer_list<std::string> &fields)
46 {
47     std::string prefix = GetPrefix(fields);
48     prefix.append("SINGLE_KEY");
49     return prefix;
50 }
51 
GetBackupKey(const std::initializer_list<std::string> & fields)52 std::string SecretKeyMetaData::GetBackupKey(const std::initializer_list<std::string> &fields)
53 {
54     std::string prefix = GetBackupPrefix(fields);
55     return prefix;
56 }
57 
GetPrefix(const std::initializer_list<std::string> & fields)58 std::string SecretKeyMetaData::GetPrefix(const std::initializer_list<std::string> &fields)
59 {
60     std::string prefix = KEY_PREFIX;
61     for (const auto &field : fields) {
62         prefix.append(Constant::KEY_SEPARATOR).append(field);
63     }
64     prefix.append(Constant::KEY_SEPARATOR);
65     return prefix;
66 }
67 
GetBackupPrefix(const std::initializer_list<std::string> & fields)68 std::string SecretKeyMetaData::GetBackupPrefix(const std::initializer_list<std::string> &fields)
69 {
70     std::string prefix = BACKUP_KEY_PREFIX;
71     for (const auto &field : fields) {
72         prefix.append(Constant::KEY_SEPARATOR).append(field);
73     }
74     prefix.append(Constant::KEY_SEPARATOR);
75     return prefix;
76 }
77 } // namespace DistributedData
78 } // namespace OHOS
79