• 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```ts
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  ```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
33FA 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**Error codes**
68
69For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
70
71| ID| Error Message|
72| -------- | -------- |
73| 13900020 | Invalid argument |
74| 13900042 | Unknown error |
75
76**Example**
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
91Calculates a hash value for a file. This API uses an asynchronous callback to return the result.
92
93**System capability**: SystemCapability.FileManagement.File.FileIO
94
95**Parameters**
96
97| Name   | Type                       | Mandatory| Description                                                        |
98| --------- | --------------------------- | ---- | ------------------------------------------------------------ |
99| path      | string                      | Yes  | Path of the file in the application sandbox.                            |
100| algorithm | string                      | Yes  | Algorithm used to calculate the hash value. The value can be **md5**, **sha1**, or **sha256**. **sha256** is recommended for security purposes.|
101| 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.|
102
103**Error codes**
104
105For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
106
107| ID| Error Message|
108| -------- | -------- |
109| 13900020 | Invalid argument |
110| 13900042 | Unknown error |
111
112**Example**
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