1 /*
2 * Copyright (C) 2022-2023 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
22 #include "algorithm_parameter.h"
23 #include "asy_key_params.h"
24 #include "blob.h"
25 #include "big_integer.h"
26 #include "cipher.h"
27 #include "detailed_dh_key_params.h"
28 #include "detailed_ecc_key_params.h"
29 #include "detailed_alg_25519_key_params.h"
30 #include "napi/native_api.h"
31 #include "napi/native_node_api.h"
32 #include "signature.h"
33
34 namespace OHOS {
35 namespace CryptoFramework {
36 #define PARAMS_NUM_ONE 1
37 #define PARAMS_NUM_TWO 2
38 #define PARAMS_NUM_THREE 3
39 #define PARAMS_NUM_FOUR 4
40
41 enum AsyncType {
42 ASYNC_CALLBACK = 1,
43 ASYNC_PROMISE = 2
44 };
45
AddUint32Property(napi_env env,napi_value object,const char * name,uint32_t value)46 inline void AddUint32Property(napi_env env, napi_value object, const char *name, uint32_t value)
47 {
48 napi_value property = nullptr;
49 napi_create_uint32(env, value, &property);
50 napi_set_named_property(env, object, name, property);
51 }
52
53 HcfBlob *GetBlobFromNapiUint8Arr(napi_env env, napi_value data);
54 HcfBlob *GetBlobFromNapiDataBlob(napi_env env, napi_value arg);
55
56 bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec);
57 napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob);
58
59 napi_value ConvertCipherBlobToNapiValue(napi_env env, HcfBlob *blob);
60
61 bool GetAsyKeySpecFromNapiValue(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec);
62 bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name, napi_value *intence);
63 napi_value ConvertBigIntToNapiValue(napi_env env, HcfBigInteger *blob);
64 napi_value ConvertEccCommParamsSpecToNapiValue(napi_env env, HcfEccCommParamsSpec *blob);
65 napi_value ConvertDhCommParamsSpecToNapiValue(napi_env env, HcfDhCommParamsSpec *blob);
66
67 bool GetStringFromJSParams(napi_env env, napi_value arg, std::string &returnStr);
68 bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt);
69 bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt);
70 bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb);
71 bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync);
72 bool isCallback(napi_env env, napi_value argv, size_t argc, size_t expectedArgc);
73 napi_value GetResourceName(napi_env env, const char *name);
74 napi_value NapiGetNull(napi_env env);
75 napi_value GenerateBusinessError(napi_env env, HcfResult errCode, const char *errMsg);
76 int32_t GetAsyKeySpecType(AsyKeySpecItem targetItemType);
77 int32_t GetSignSpecType(SignSpecItem targetItemType);
78 int32_t GetCipherSpecType(CipherSpecItem targetItemType);
79 } // namespace CryptoFramework
80 } // namespace OHOS
81 #endif
82