• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.hash (文件哈希处理)
2
3该模块提供文件哈希处理能力,对文件内容进行哈希处理。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import Hash from '@ohos.file.hash';
13```
14
15## 使用说明
16
17使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:
18
19**Stage模型**
20
21  ```ts
22  import UIAbility from '@ohos.app.ability.UIAbility';
23  import window from '@ohos.window';
24
25  export default class EntryAbility extends UIAbility {
26    onWindowStageCreate(windowStage: window.WindowStage) {
27      let context = this.context;
28      let pathDir = context.filesDir;
29    }
30  }
31  ```
32
33**FA模型**
34
35  ```js
36  import featureAbility from '@ohos.ability.featureAbility';
37
38  let context = featureAbility.getContext();
39  context.getFilesDir().then((data) => {
40    let pathDir = data;
41  })
42  ```
43
44FA模型context的具体获取方法参见[FA模型](js-apis-inner-app-context.md#Context模块)。
45
46## Hash.hash
47
48hash(path: string, algorithm: string): Promise<string>
49
50计算文件的哈希值,使用Promise异步回调。
51
52**系统能力**:SystemCapability.FileManagement.File.FileIO
53
54**参数:**
55
56| 参数名    | 类型   | 必填 | 说明                                                         |
57| --------- | ------ | ---- | ------------------------------------------------------------ |
58| path      | string | 是   | 待计算哈希值文件的应用沙箱路径。                             |
59| algorithm | string | 是   | 哈希计算采用的算法。可选 "md5"、"sha1" 或 "sha256"。建议采用安全强度更高的 "sha256"。 |
60
61**返回值:**
62
63  | 类型                    | 说明                         |
64  | --------------------- | -------------------------- |
65  | Promise<string> | Promise对象。返回文件的哈希值。表示为十六进制数字串,所有字母均大写。 |
66
67**错误码:**
68
69以下错误码的详细介绍请参见[基础文件IO错误码](../errorcodes/errorcode-filemanagement.md#基础文件io错误码)。
70
71| 错误码ID | 错误信息 |
72| -------- | -------- |
73| 13900020 | Invalid argument |
74| 13900042 | Unknown error |
75
76**示例:**
77
78  ```ts
79  let filePath = pathDir + "/test.txt";
80  Hash.hash(filePath, "sha256").then((str: string) => {
81    console.info("calculate file hash succeed:" + str);
82  }).catch((err: BusinessError) => {
83    console.info("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
84  });
85  ```
86
87## Hash.hash
88
89hash(path: string, algorithm: string, callback: AsyncCallback<string>): void
90
91计算文件的哈希值,使用callback异步回调。
92
93**系统能力**:SystemCapability.FileManagement.File.FileIO
94
95**参数:**
96
97| 参数名    | 类型                        | 必填 | 说明                                                         |
98| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
99| path      | string                      | 是   | 待计算哈希值文件的应用沙箱路径。                             |
100| algorithm | string                      | 是   | 哈希计算采用的算法。可选 "md5"、"sha1" 或 "sha256"。建议采用安全强度更高的 "sha256"。 |
101| callback  | AsyncCallback<string> | 是   | 异步计算文件哈希操作之后的回调函数(其中给定文件哈希值表示为十六进制数字串,所有字母均大写)。 |
102
103**错误码:**
104
105以下错误码的详细介绍请参见[基础文件IO错误码](../errorcodes/errorcode-filemanagement.md#基础文件io错误码)。
106
107| 错误码ID | 错误信息 |
108| -------- | -------- |
109| 13900020 | Invalid argument |
110| 13900042 | Unknown error |
111
112**示例:**
113
114  ```ts
115  let filePath = pathDir + "/test.txt";
116  Hash.hash(filePath, "sha256", (err: BusinessError, str: string) => {
117    if (err) {
118      console.info("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
119    } else {
120      console.info("calculate file hash succeed:" + str);
121    }
122  });
123  ```
124