• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.hash (File Hash Processing)
2
3The **fileHash** module implements hash processing on files.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> - The APIs of this module support processing of error codes. For details, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
9
10## Modules to Import
11
12```js
13import Hash from '@ohos.file.hash';
14```
15
16## Guidelines
17
18Before using the APIs provided by this module to perform operations on a file or directory, obtain the path of the file or directory in the application sandbox as follows:
19
20**Stage Model**
21
22 ```js
23import UIAbility from '@ohos.app.ability.UIAbility';
24
25export default class EntryAbility extends UIAbility {
26    onWindowStageCreate(windowStage) {
27        let context = this.context;
28        let pathDir = context.filesDir;
29    }
30}
31 ```
32
33**FA Model**
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
44For details about how to obtain the FA model context, see [Context](js-apis-inner-app-context.md#context).
45
46## Hash.hash
47
48hash(path: string, algorithm: string): Promise<string>
49
50Calculates a hash value for a file. This API uses a promise to return the result.
51
52**System capability**: SystemCapability.FileManagement.File.FileIO
53
54**Parameters**
55
56| Name   | Type  | Mandatory| Description                                                        |
57| --------- | ------ | ---- | ------------------------------------------------------------ |
58| path      | string | Yes  | Path of the file in the application sandbox.                            |
59| algorithm | string | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
60
61**Return value**
62
63  | Type                   | Description                        |
64  | --------------------- | -------------------------- |
65  | Promise<string> | Promise used to return the hash value. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
66
67**Example**
68
69  ```js
70  let filePath = pathDir + "/test.txt";
71  Hash.hash(filePath, "sha256").then((str) => {
72    console.info("calculate file hash succeed:" + str);
73  }).catch((err) => {
74    console.info("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
75  });
76  ```
77
78## Hash.hash
79
80hash(path: string, algorithm: string, callback: AsyncCallback<string>): void
81
82Calculates a hash value for a file. This API uses an asynchronous callback to return the result.
83
84**System capability**: SystemCapability.FileManagement.File.FileIO
85
86**Parameters**
87
88| Name   | Type                       | Mandatory| Description                                                        |
89| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
90| path      | string                      | Yes  | Path of the file in the application sandbox.                            |
91| algorithm | string                      | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
92| callback  | AsyncCallback<string> | Yes  | Callback used to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
93
94**Example**
95  ```js
96  let filePath = pathDir + "/test.txt";
97  Hash.hash(filePath, "sha256", (err, str) => {
98    if (err) {
99      console.info("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
100    } else {
101      console.info("calculate file hash succeed:" + str);
102    }
103  });
104  ```
105