• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "hash_ani.h"
17 
18 #include "error_handler.h"
19 #include "filemgmt_libhilog.h"
20 #include "hash_core.h"
21 #include "type_converter.h"
22 
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27 
28 using namespace std;
29 using namespace OHOS::FileManagement::ModuleFileIO;
30 
HashSync(ani_env * env,ani_class clazz,ani_string path,ani_string algorithm)31 ani_string HashAni::HashSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string algorithm)
32 {
33     auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path);
34     if (!succPath) {
35         HILOGE("Invalid path");
36         ErrorHandler::Throw(env, EINVAL);
37         return nullptr;
38     }
39 
40     auto [succAlg, algType] = TypeConverter::ToUTF8String(env, algorithm);
41     if (!succAlg) {
42         HILOGE("Invalid algorithm");
43         ErrorHandler::Throw(env, EINVAL);
44         return nullptr;
45     }
46 
47     auto ret = HashCore::DoHash(srcPath, algType);
48     if (!ret.IsSuccess()) {
49         HILOGE("DoHash failed");
50         const auto &err = ret.GetError();
51         ErrorHandler::Throw(env, err);
52         return nullptr;
53     }
54 
55     const auto &res = ret.GetData().value();
56     auto [succ, result] = TypeConverter::ToAniString(env, res);
57 
58     if (!succ) {
59         HILOGE("Convert hash value to ani string failed");
60         ErrorHandler::Throw(env, UNKNOWN_ERR);
61         return nullptr;
62     }
63     return result;
64 }
65 
66 } // namespace ANI
67 } // namespace ModuleFileIO
68 } // namespace FileManagement
69 } // namespace OHOS