该模块提供文件哈希处理能力,对文件内容进行哈希处理。
说明
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
导入模块
import { hash } from '@kit.CoreFileKit';
使用说明
使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
let context = this.context;
let pathDir = context.filesDir;
}
}
hash.hash
hash(path: string, algorithm: string): Promise
计算文件的哈希值,使用Promise异步回调。
元服务API:从API version 11开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.File.FileIO
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
path | string | 是 | 待计算哈希值文件的应用沙箱路径。 |
algorithm | string | 是 | 哈希计算采用的算法。可选 “md5”、“sha1” 或 “sha256”。建议采用安全强度更高的 “sha256”。 |
返回值:
类型 | 说明 |
---|---|
Promise | Promise对象。返回文件的哈希值。表示为十六进制数字串,所有字母均大写。 |
错误码ID | 错误信息 |
---|---|
13900020 | Invalid argument |
13900042 | Unknown error |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let filePath = pathDir + "/test.txt";
hash.hash(filePath, "sha256").then((str: string) => {
console.info("calculate file hash succeed:" + str);
}).catch((err: BusinessError) => {
console.error("calculate file hash failed with error message: " + err.message + ", error code: " + err.code);
});
hash.hash
hash(path: string, algorithm: string, callback: AsyncCallback): void
计算文件的哈希值,使用callback异步回调。
元服务API:从API version 11开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.File.FileIO
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
path | string | 是 | 待计算哈希值文件的应用沙箱路径。 |
algorithm | string | 是 | 哈希计算采用的算法。可选 “md5”、“sha1” 或 “sha256”。建议采用安全强度更高的 “sha256”。 |
callback | AsyncCallback | 是 | 异步计算文件哈希操作之后的回调函数(其中给定文件哈希值表示为十六进制数字串,所有字母均大写)。 |
错误码ID | 错误信息 |
---|---|
13900020 | Invalid argument |
13900042 | Unknown error |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let filePath = pathDir + "/test.txt";
hash.hash(filePath, "sha256", (err: BusinessError, str: string) => {
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);
}
});
hash.createHash12+
createHash(algorithm: string): HashStream;
创建并返回 HashStream 对象,该对象可用于使用给定的 algorithm 生成哈希摘要。
系统能力:SystemCapability.FileManagement.File.FileIO
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
algorithm | string | 是 | 哈希计算采用的算法。可选 “md5”、“sha1” 或 “sha256”。建议采用安全强度更高的 “sha256”。 |
返回值:
类型 | 说明 |
---|---|
[HashStream] | HashStream 类的实例。 |
示例:
// pages/xxx.ets
import { fileIo as fs } from '@kit.CoreFileKit';
function hashFileWithStream() {
const filePath = pathDir + "/test.txt";
// 创建文件可读流
const rs = fs.createReadStream(filePath);
// 创建哈希流
const hs = hash.createHash('sha256');
rs.on('data', (emitData) => {
const data = emitData?.data;
hs.update(new Uint8Array(data?.split('').map((x: string) => x.charCodeAt(0))).buffer);
});
rs.on('close', async () => {
const hashResult = hs.digest();
const fileHash = await hash.hash(filePath, 'sha256');
console.info(`hashResult: ${hashResult}, fileHash: ${fileHash}`);
});
}
HashStream12+
HashStream 类是用于创建数据的哈希摘要的实用工具。
update12+
update(data: ArrayBuffer): void
使用给定的 data 更新哈希内容,可多次调用。
系统能力:SystemCapability.FileManagement.File.FileIO
示例:
// 创建哈希流
const hs = hash.createHash('sha256');
hs.update(new Uint8Array('1234567890'?.split('').map((x: string) => x.charCodeAt(0))).buffer);
hs.update(new Uint8Array('abcdefg'?.split('').map((x: string) => x.charCodeAt(0))).buffer);
const hashResult = hs.digest();
// 88A00F46836CD629D0B79DE98532AFDE3AEAD79A5C53E4848102F433046D0106
console.info(`hashResult: ${hashResult}`);
digest12+
digest(): string
计算传给被哈希的所有数据的摘要。
系统能力:SystemCapability.FileManagement.File.FileIO
示例:
// 创建哈希流
const hs = hash.createHash('sha256');
hs.update(new Uint8Array('1234567890'?.split('').map((x: string) => x.charCodeAt(0))).buffer);
hs.update(new Uint8Array('abcdefg'?.split('').map((x: string) => x.charCodeAt(0))).buffer);
const hashResult = hs.digest();
// 88A00F46836CD629D0B79DE98532AFDE3AEAD79A5C53E4848102F433046D0106
console.info(`hashResult: ${hashResult}`);
最后呢
很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。
而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点
如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。
针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细鸿蒙(OpenHarmony )手册(共计1236页)与鸿蒙(OpenHarmony )开发入门视频,帮助大家在技术的道路上更进一步。
- 《鸿蒙 (OpenHarmony)开发学习视频》
- 《鸿蒙生态应用开发V2.0白皮书》
- 《鸿蒙 (OpenHarmony)开发基础到实战手册》
- OpenHarmony北向、南向开发环境搭建
- 《鸿蒙开发基础》
- 《鸿蒙开发进阶》
- 《鸿蒙开发实战》
总结
鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。
并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行! 自↓↓↓拿