W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
該模塊提供文件哈希處理能力,對文件內(nèi)容進(jìn)行哈希處理。
本模塊首批接口從API version 9開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
使用該功能模塊對文件/目錄進(jìn)行操作前,需要先獲取其應(yīng)用沙箱路徑,獲取方式及其接口用法請參考:
Stage模型
- import UIAbility from '@ohos.app.ability.UIAbility';
- export default class EntryAbility extends UIAbility {
- onWindowStageCreate(windowStage) {
- let context = this.context;
- let pathDir = context.filesDir;
- }
- }
FA模型
- import featureAbility from '@ohos.ability.featureAbility';
- let context = featureAbility.getContext();
- context.getFilesDir().then((data) => {
- let pathDir = data;
- })
FA模型context的具體獲取方法參見FA模型。
hash(path: string, algorithm: string): Promise<string>
計(jì)算文件的哈希值,使用Promise異步回調(diào)。
系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
path | string | 是 | 待計(jì)算哈希值文件的應(yīng)用沙箱路徑。 |
algorithm | string | 是 | 哈希計(jì)算采用的算法??蛇x "md5"、"sha1" 或 "sha256"。建議采用安全強(qiáng)度更高的 "sha256"。 |
返回值:
類型 | 說明 |
---|---|
Promise<string> | Promise對象。返回文件的哈希值。表示為十六進(jìn)制數(shù)字串,所有字母均大寫。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請參見基礎(chǔ)文件IO錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
13900020 | Invalid argument |
13900042 | Unknown error |
示例:
- let filePath = pathDir + "/test.txt";
- Hash.hash(filePath, "sha256").then((str) => {
- console.info("calculate file hash succeed:" + str);
- }).catch((err) => {
- console.error("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
- });
hash(path: string, algorithm: string, callback: AsyncCallback<string>): void
計(jì)算文件的哈希值,使用callback異步回調(diào)。
系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
path | string | 是 | 待計(jì)算哈希值文件的應(yīng)用沙箱路徑。 |
algorithm | string | 是 | 哈希計(jì)算采用的算法。可選 "md5"、"sha1" 或 "sha256"。建議采用安全強(qiáng)度更高的 "sha256"。 |
callback | AsyncCallback<string> | 是 | 異步計(jì)算文件哈希操作之后的回調(diào)函數(shù)(其中給定文件哈希值表示為十六進(jìn)制數(shù)字串,所有字母均大寫)。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請參見基礎(chǔ)文件IO錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
13900020 | Invalid argument |
13900042 | Unknown error |
示例:
- let filePath = pathDir + "/test.txt";
- Hash.hash(filePath, "sha256", (err, str) => {
- if (err) {
- console.error("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
- } else {
- console.info("calculate file hash succeed:" + str);
- }
- });
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: