• 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
9## Modules to Import
10
11```js
12import Hash from '@ohos.file.hash';
13```
14
15## Guidelines
16
17Before 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:
18
19Stage Model
20
21 ```js
22import UIAbility from '@ohos.app.ability.UIAbility';
23
24export default class EntryAbility extends UIAbility {
25    onWindowStageCreate(windowStage) {
26        let context = this.context;
27        let pathDir = context.filesDir;
28    }
29}
30 ```
31
32FA Model
33
34 ```js
35 import featureAbility from '@ohos.ability.featureAbility';
36
37 let context = featureAbility.getContext();
38 context.getFilesDir().then((data) => {
39      let pathDir = data;
40 })
41 ```
42
43For details about how to obtain the FA model context, see [Context](js-apis-inner-app-context.md#context).
44
45## Hash.hash
46
47hash(path: string, algorithm: string): Promise<string>
48
49Calculates a hash value for a file. This API uses a promise to return the result.
50
51**System capability**: SystemCapability.FileManagement.File.FileIO
52
53**Parameters**
54
55| Name   | Type  | Mandatory| Description                                                        |
56| --------- | ------ | ---- | ------------------------------------------------------------ |
57| path      | string | Yes  | Path of the file in the application sandbox.                            |
58| algorithm | string | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
59
60**Return value**
61
62  | Type                   | Description                        |
63  | --------------------- | -------------------------- |
64  | Promise<string> | Promise used to return the hash value. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
65
66**Error codes**
67
68For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
69
70| ID| Error Message|
71| -------- | -------- |
72| 13900020 | Invalid argument |
73| 13900042 | Unknown error |
74
75**Example**
76
77  ```js
78  let filePath = pathDir + "/test.txt";
79  Hash.hash(filePath, "sha256").then((str) => {
80    console.info("calculate file hash succeed:" + str);
81  }).catch((err) => {
82    console.info("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
83  });
84  ```
85
86## Hash.hash
87
88hash(path: string, algorithm: string, callback: AsyncCallback<string>): void
89
90Calculates a hash value for a file. This API uses an asynchronous callback to return the result.
91
92**System capability**: SystemCapability.FileManagement.File.FileIO
93
94**Parameters**
95
96| Name   | Type                       | Mandatory| Description                                                        |
97| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
98| path      | string                      | Yes  | Path of the file in the application sandbox.                            |
99| algorithm | string                      | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
100| callback  | AsyncCallback<string> | Yes  | Callback invoked to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.|
101
102**Error codes**
103
104For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
105
106| ID| Error Message|
107| -------- | -------- |
108| 13900020 | Invalid argument |
109| 13900042 | Unknown error |
110
111**Example**
112
113  ```js
114  let filePath = pathDir + "/test.txt";
115  Hash.hash(filePath, "sha256", (err, str) => {
116    if (err) {
117      console.info("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
118    } else {
119      console.info("calculate file hash succeed:" + str);
120    }
121  });
122  ```
123