@ohos.file.hash (File Hash Processing)
The fileHash module implements hash processing on files.
NOTE
- 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.
- The APIs of this module support processing of error codes. For details, see File Management Error Codes.
Modules to Import
import Hash from '@ohos.file.hash';
Guidelines
Before 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:
Stage Model
import UIAbility from '@ohos.app.ability.UIAbility';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage) {
let context = this.context;
let pathDir = context.filesDir;
}
}
FA Model
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
context.getFilesDir().then((data) => {
let pathDir = data;
})
For details about how to obtain the FA model context, see Context.
Hash.hash
hash(path: string, algorithm: string): Promise<string>
Calculates a hash value for a file. This API uses a promise to return the result.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
path | string | Yes | Path of the file in the application sandbox. |
algorithm | string | Yes | Algorithm used to calculate the hash value. The value can be md5, sha1, or sha256. sha256 is recommended for security purposes. |
Return value
Type | Description |
---|---|
Promise<string> | Promise used to return the hash value. The hash value is a hexadecimal string consisting of digits and uppercase letters. |
Example
let filePath = pathDir + "/test.txt";
Hash.hash(filePath, "sha256").then((str) => {
console.info("calculate file hash succeed:" + str);
}).catch((err) => {
console.info("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
});
Hash.hash
hash(path: string, algorithm: string, callback: AsyncCallback<string>): void
Calculates a hash value for a file. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
path | string | Yes | Path of the file in the application sandbox. |
algorithm | string | Yes | Algorithm used to calculate the hash value. The value can be md5, sha1, or sha256. sha256 is recommended for security purposes. |
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. |
Example
let filePath = pathDir + "/test.txt";
Hash.hash(filePath, "sha256", (err, str) => {
if (err) {
console.info("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("calculate file hash succeed:" + str);
}
});