• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HCF_NAPI_UILTS_H
17 #define HCF_NAPI_UILTS_H
18 
19 #include <cstdint>
20 #include <string>
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "blob.h"
24 #include "cert_chain_validator.h"
25 #include "algorithm_parameter.h"
26 #include "cipher.h"
27 
28 namespace OHOS {
29 namespace CryptoFramework {
30 #define PARAMS_NUM_ONE 1
31 #define PARAMS_NUM_TWO 2
32 #define PARAMS_NUM_THREE 3
33 #define PARAMS_NUM_FOUR 4
34 
35 enum AsyncType {
36     ASYNC_CALLBACK = 1,
37     ASYNC_PROMISE = 2
38 };
39 
AddUint32Property(napi_env env,napi_value object,const char * name,uint32_t value)40 inline void AddUint32Property(napi_env env, napi_value object, const char *name, uint32_t value)
41 {
42     napi_value property = nullptr;
43     napi_create_uint32(env, value, &property);
44     napi_set_named_property(env, object, name, property);
45 }
46 
47 HcfBlob *GetBlobFromNapiValue(napi_env env, napi_value arg);
48 bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec);
49 napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob);
50 
51 bool GetStringFromJSParams(napi_env env, napi_value arg, std::string &returnStr, bool isCertFunc);
52 bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt, bool isCertFunc);
53 bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt, bool isCertFunc);
54 bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb, bool isCertFunc);
55 bool GetEncodingBlobFromValue(napi_env env, napi_value object, HcfEncodingBlob **encodingBlob);
56 bool GetCertChainFromValue(napi_env env, napi_value object, HcfCertChainData **certChainData);
57 bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync, bool isCertFunc);
58 napi_value GetResourceName(napi_env env, const char *name);
59 napi_value GenerateArrayBuffer(napi_env env, uint8_t *data, uint32_t size);
60 napi_value NapiGetNull(napi_env env);
61 napi_value ConvertArrayToNapiValue(napi_env env, HcfArray *array);
62 napi_value ConvertEncodingBlobToNapiValue(napi_env env, HcfEncodingBlob *encodingBlob);
63 napi_value GenerateBusinessError(napi_env env, int32_t errCode, const char *errMsg, bool isCertFunc);
64 }  // namespace CryptoFramework
65 }  // namespace OHOS
66 #endif
67