npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cordova-ohos/cordova-plugin-file-md5

v0.3.4

Published

Cordova file md5 Plugin

Downloads

754

Readme

cordova-plugin-file-md5

zh-CN en

本项目基于 [email protected] 开发,本文档重点阐述其在 OpenHarmony(OHOS)系统中的具体应用。

简介

cordova-plugin-file-md5 是一款轻量、高效的文件 MD5 计算插件,基于各平台原生 API 实现,规避纯前端计算的性能瓶颈,支持本地文件、assets 目录文件及远程文件(下载后计算)的 MD5 哈希值计算,具备计算速度快、内存占用低、跨平台兼容等特点。可广泛应用于文件完整性校验、文件唯一标识生成、断点续传校验、缓存文件管理等场景。核心功能包括:

  • 调用原生 API 计算文件 MD5,性能优于前端计算

  • 支持本地沙盒文件、外部存储文件、assets 目录文件的 MD5 计算

  • 支持远程文件下载后计算 MD5,适配各类网络文件场景

  • 可配置进度回调,实时反馈 MD5 计算进度

  • 提供清晰的错误信息,便于调试与问题定位

支持平台

  • Android:API 19 及以上(Android 4.4+)

  • iOS:10.0 及以上

  • OHOS:5.0+

下载安装

通过 hcordova CLI 即可快速安装插件,支持从 npm 仓库或 GitCode 仓库获取。

从 npm 安装(推荐)

# 使用 hcordova CLI 安装
# 安装 hcordova 命令化工具
npm install -g hcordova

# 全平台安装插件
hcordova plugin add cordova-plugin-file-md5

指定平台安装 ohos

仅为 OHOS 平台安装插件:

# 仅安装到 OHOS 平台
hcordova plugin add cordova-plugin-file-md5 --platform ohos

从 GitCode 仓库安装

仅为 OHOS 平台安装插件:

# 仅安装到 OHOS 平台
# 默认分支安装
hcordova plugin add https://gitcode.com/CPF-Cordova/cordova-plugin-file-md5.git --platform ohos

# 指定标签/分支安装
hcordova plugin add https://gitcode.com/CPF-Cordova/cordova-plugin-file-md5.git@develop --platform ohos

离线安装(本地包)

适用于无网络环境,先下载插件包到本地,再执行离线安装:

# 下载插件包到本地(示例路径:~/Downloads/cordova-plugin-file-md5)
# 执行离线安装
hcordova plugin add ~/Downloads/cordova-plugin-file-md5 --platform ohos

安装后验证

安装完成后,可通过以下命令验证插件是否成功添加到项目中:

# 查看已安装的插件列表,若包含本插件 ID 则表示插件已成功安装
hcordova plugin list

卸载

# Cordova CLI 全平台卸载
hcordova plugin remove cordova-plugin-file-md5 

# 指定平台卸载
hcordova plugin remove cordova-plugin-file-md5 --platform ohos

约束与限制

  • 依赖插件:依赖 cordova-plugin-file 插件(安装时会自动依赖,无需手动安装)

  • 调用时机:所有 API 需在 deviceready 事件触发后调用,避免原生接口未初始化导致报错

  • 文件权限:OHOS 沙盒文件计算无需额外申请权限

  • 文件大小:支持大文件 MD5 计算,建议对超过 100MB 的文件启用进度回调,提升用户体验

  • 远程文件:计算远程文件 MD5 时,需确保网络通畅,且文件可正常下载(无跨域、权限限制)

兼容性

支持:

| 项目 | 版本/信息 | |-----|--------| | SDK | API12+ | | IDE | DevEco Studio 5.0+ | | ROM | 5.1+ | | Emulator | OpenHarmony 6.0+ |

在以下版本中已测试通过:

| 项目 | 版本/信息 | |-----|--------| | @cordova-ohos/ohos | 14.0.1-ohos-14.0.1 | | SDK | 5.0.0(12) | | IDE | DevEco Studio 6.0.13.200 | | ROM | 5.1.0.120 SP3 | | Emulator | OpenHarmony 6.0.1(21) |

使用示例

以下提供两种常用场景的使用示例,适配 OHOS 平台,可直接复制到项目中调试使用,所有示例均需在 deviceready 事件触发后执行。

示例 1:基础使用(本地文件 MD5 计算)

适用于已获取本地文件 FileEntry 对象的场景,快速计算文件 MD5 值,可根据需求添加进度回调。

// 成功回调:获取文件 MD5 值
function successCallback(md5sum) {
    console.log("文件 MD5 值:" + md5sum);
}

// 失败回调:输出错误信息
function errorCallback(error) {
    console.error("MD5 计算失败:" + error);
}

// 进度回调(可选):显示计算进度
function progressCallback(progress) {
    console.log("MD5 计算进度:" + progress + "%");
}

// 假设已通过 cordova-plugin-file 获取本地文件的 FileEntry 对象(示例:本地缓存目录文件)
window.resolveLocalFileSystemURL(
    cordova.file.cacheDirectory + "test.txt",
    function(fileEntry) {
        // 调用 API 计算 MD5
        md5chksum.file(fileEntry, successCallback, errorCallback, progressCallback);
    },
    function(error) {
        console.error("获取本地文件失败:" + error);
    }
);

示例 2:完整流程(远程文件下载 + MD5 计算)

实现「远程文件下载 → 保存至本地缓存目录 → 计算 MD5 值」的完整业务流程,包含异常处理,可直接复用。

/**
 * 完整流程:下载远程文件 → 保存至本地缓存目录 → 计算文件 MD5 值
 */
function fileMd5() {
    // 1. 写入文件(将下载的 Blob 数据写入本地文件)
    function writeFile(fileEntry, dataObj, isAppend = false) {
        fileEntry.createWriter(function (fileWriter) {
            // 写入完成回调:开始计算 MD5
            fileWriter.onwriteend = function() {
                console.log("文件写入完成,开始计算 MD5...");
                // 调用 MD5 计算 API
                md5chksum.file(
                    fileEntry,
                    function(md5Value) {
                        // 计算成功:更新页面显示 MD5 值
                        document.getElementById("md5Value").innerHTML = "文件 MD5 值:" + md5Value;
                    },
                    function(errorInfo) {
                        // 计算失败:显示错误信息
                        document.getElementById("md5Value").innerHTML = "MD5 计算失败:" + errorInfo;
                    },
                    function(progress) {
                        // 进度回调:显示计算进度
                        document.getElementById("md5Progress").innerHTML = "计算进度:" + progress + "%";
                    }
                );
            };

            // 写入错误回调
            fileWriter.onerror = function(e) {
                console.error("文件写入失败:" + e.toString());
                document.getElementById("md5Value").innerHTML = "文件写入失败:" + e.toString();
            };

            // 写入数据(若需追加内容,设置 isAppend 为 true)
            if (isAppend) {
                fileWriter.seek(fileWriter.length); // 定位到文件末尾
            }
            fileWriter.write(dataObj);
        });
    }

    // 2. 保存文件到指定目录
    function saveFile(dirEntry, fileData, fileName) {
        // 创建/获取文件(create: true 表示不存在则创建)
        dirEntry.getFile(
            fileName,
            { create: true, exclusive: false },
            function (fileEntry) {
                writeFile(fileEntry, fileData); // 调用写入文件方法
            },
            function(error) {
                console.error("获取文件失败:" + error);
                document.getElementById("md5Value").innerHTML = "获取文件失败:" + error;
            }
        );
    }

    // 3. 下载远程文件(以 Cordova 官方图片为例,可替换为自定义文件地址)
    function getSampleFile(dirEntry) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'https://cordova.apache.org/static/img/cordova_bot.png', true);
        xhr.responseType = 'blob'; // 以 Blob 格式获取文件数据

        // 下载成功回调
        xhr.onload = function() {
            if (this.status == 200) {
                var blob = new Blob([this.response], { type: 'image/png' });
                // 保存文件到指定目录,文件名为 downloadedImage.png
                saveFile(dirEntry, blob, "downloadedImage.png");
            } else {
                console.error("文件下载失败,状态码:" + this.status);
                document.getElementById("md5Value").innerHTML = "文件下载失败,状态码:" + this.status;
            }
        };

        // 下载错误回调
        xhr.onerror = function() {
            console.error("文件下载失败:网络错误");
            document.getElementById("md5Value").innerHTML = "文件下载失败:网络错误";
        };

        xhr.send(); // 发起下载请求
    }

    // 4. 初始化:获取本地缓存目录,创建子目录用于保存下载文件
    window.resolveLocalFileSystemURL(
        cordova.file.cacheDirectory,
        function(dirEntry) {
            // 显示下载中提示
            document.getElementById("md5Value").innerHTML = "正在下载文件...";
            document.getElementById("md5Progress").innerHTML = "下载进度:0%";
            // 创建子目录(magongshou/import),用于保存下载的文件
            dirEntry.getDirectory(
                "magongshou/import",
                { create: true }, // 不存在则创建
                function (dir) {
                    getSampleFile(dir); // 开始下载文件
                },
                function(error) {
                    console.error("创建目录失败:" + error);
                    document.getElementById("md5Value").innerHTML = "创建目录失败:" + error;
                }
            );
        },
        function(error) {
            console.error("获取缓存目录失败:" + error);
            document.getElementById("md5Value").innerHTML = "获取缓存目录失败:" + error;
        }
    );
}

// 调用方法,触发完整流程(需在 deviceready 事件后调用)
document.addEventListener("deviceready", function() {
    fileMd5();
}, false);

使用说明

本插件核心功能为计算文件 MD5 哈希值,支持本地文件、远程下载文件(需先下载至本地),所有操作需在 deviceready 事件触发后执行,核心 API 及参数说明如下。

核心 API 说明

插件唯一核心 API 为 md5chksum.file(),用于计算指定文件的 MD5 值,提供成功、失败及进度回调,满足不同场景下的使用需求。

方法签名

md5chksum.file(fileEntry, successCallback, errorCallback, progressCallback);

参数说明

| 参数名 | 类型 | 说明 | |---|---|---| | fileEntry | FileEntry | 必填,要计算 MD5 的文件对象,可通过 cordova-plugin-file 插件获取 | | successCallback | Function | 必填,计算成功回调,参数为 md5sum(字符串类型,文件的 MD5 哈希值) | | errorCallback | Function | 必填,计算失败回调,参数为 error(字符串类型,错误信息) | | progressCallback | Function | 可选,进度回调,参数为 progress(数字类型,0-100,代表计算进度百分比) |

目录结构

cordova-plugin-file-md5/
├── src/                          # 源代码目录
│   └── main/                     # 主要源代码
│       └── cpp/                  # C++ 原生代码
│           └── file/             # 文件模块
│               ├── md5chksum.cpp # 文件 MD5 校验功能的 C++ 实现
│               └── md5chksum.h   # 文件 MD5 校验功能的头文件
├── www/                          # Web 资源目录
│   └── md5chksum.js              # JavaScript 文件 MD5 校验接口(Cordova 桥接层)
├── .gitignore                    # Git 忽略文件配置
├── LICENSE                       # 项目开源许可证
├── OAT.xml                       # OpenHarmony 审核配置文件
├── package.json                  # Node.js 包配置
├── plugin.xml                    # Cordova 插件描述文件(核心配置)
└── README.md                     # 项目说明文档

贡献代码

使用过程中发现任何问题都可以提 Issue ,当然也非常欢迎发 PR 共建。

许可证

本插件基于 Apache License 2.0 开源,详见 LICENSE 文件。

官方资源