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 #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_SERVICE_CRYPTO_CRYPTO_MANAGER_H
16 #define OHOS_DISTRIBUTED_DATA_SERVICES_SERVICE_CRYPTO_CRYPTO_MANAGER_H
17 #include <cstdint>
18 #include <vector>
19 #include "visibility.h"
20 
21 namespace OHOS::DistributedData {
22 class API_EXPORT CryptoManager {
23 public:
24     static CryptoManager &GetInstance();
25     int32_t GenerateRootKey();
26     int32_t CheckRootKey();
27     std::vector<uint8_t> Encrypt(const std::vector<uint8_t> &key);
28     bool Decrypt(std::vector<uint8_t> &source, std::vector<uint8_t> &key);
29 
30     enum ErrCode : int32_t {
31         SUCCESS,
32         NOT_EXIST,
33         ERROR,
34     };
35 private:
36     static constexpr const char *ROOT_KEY_ALIAS = "distributed_db_root_key";
37     static constexpr const char *HKS_BLOB_TYPE_NONCE = "Z5s0Bo571KoqwIi6";
38     static constexpr const char *HKS_BLOB_TYPE_AAD = "distributeddata";
39     static constexpr int KEY_SIZE = 32;
40     static constexpr int HOURS_PER_YEAR = (24 * 365);
41     CryptoManager();
42     ~CryptoManager();
43     std::vector<uint8_t> vecRootKeyAlias_{};
44     std::vector<uint8_t> vecNonce_{};
45     std::vector<uint8_t> vecAad_{};
46 };
47 } // namespace OHOS::DistributedData
48 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_SERVICE_CRYPTO_CRYPTO_MANAGER_H
49