harmony 鸿蒙@ohos.file.hash (File Hash Processing)

  • 2023-02-17
  • 浏览 (546)

@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.

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';
  import window from '@ohos.window';

  export default class EntryAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.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.

Error codes

For details about the error codes, see Basic File IO Error Codes.

ID Error Message
13900020 Invalid argument
13900042 Unknown error

Example

  let filePath = pathDir + "/test.txt";
  Hash.hash(filePath, "sha256").then((str: string) => {
    console.info("calculate file hash succeed:" + str);
  }).catch((err: BusinessError) => {
    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 invoked to return the hash value obtained. The hash value is a hexadecimal string consisting of digits and uppercase letters.

Error codes

For details about the error codes, see Basic File IO Error Codes.

ID Error Message
13900020 Invalid argument
13900042 Unknown error

Example

  let filePath = pathDir + "/test.txt";
  Hash.hash(filePath, "sha256", (err: BusinessError, str: string) => {
    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);
    }
  });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞