更新记录

1.0.1(2025-03-17)

修改readme.md

1.0.0(2025-03-17)

初次发布,支持六个方法,可同步异步进行哈希摘要验证


平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
HBuilderX 4.0,Android:4.4,iOS:不支持,HarmonyNext:不支持 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序 鸿蒙元服务
× × × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

使用前说明

本插件在uni-app原生应用测试通过,插件理论上支持uni-app x,可自行测试。 使用插件前须确保应用的读取文件权限已开启。

插件导入

import {
        hashWithUrl, //同步将本地文件哈希摘要和网络摘要文件校验
        hashWithFile, //同步将本地文件哈希摘要和本地摘要文件校验
        hashWithString, //同步将本地文件哈希摘要和目标摘要校验
        hashWithUrlAsync, //异步将本地文件哈希摘要和网络摘要文件校验
        hashWithFileAsync, //异步将本地文件哈希摘要和本地摘要文件校验
        hashWithStringAsync //异步将本地文件哈希摘要和目标摘要校验
    } from '@/uni_modules/cfit-hashverify'

插件使用

const file = 'file:///storage/emulated/0/Android/data/io.dcloud.Hbuilder/maven-metadata.xml'; //本地文件,必须头部加上file://
const url = 'https://repo1.maven.org/maven2/log4j/log4j/1.2.9/maven-metadata.xml.sha1'; //远程校验文件,内部至少含有校验值
const hashfile = 'file:///storage/emulated/0/Android/data/io.dcloud.Hbuilder/maven-metadata.xml.sha1'; //本地校验文件,内部至少含有校验值
const algorithm = "sha1"; //校验算法,支持md5, sha1, sha256, sha512,小写传入
const hash = "41f582c44774800ea05547fc155cf63ce60d082e"; //目标摘要值
console.log(hashWithUrl(file, url, algorithm)); //同步将本地文件哈希摘要和网络摘要文件校验
console.log(hashWithFile(file, hashfile, algorithm)); //同步将本地文件哈希摘要和本地摘要文件校验
console.log(hashWithString(file, hash, algorithm)); //同步将本地文件哈希摘要和目标摘要校验
hashWithUrlAsync(file, url, algirithm) //异步将本地文件哈希摘要和网络摘要文件校验
    .then(isValid => {
        console.log(isValid);
    })
    .catch(error => console.error(error));
hashWithFileAsync(file, hashfile, algorithm) //异步将本地文件哈希摘要和本地摘要文件校验
    .then(isValid => {
        console.log(isValid);
    })
    .catch(error => console.error(error));
hashWithStringAsync(file, hash, algorithm) //异步将本地文件哈希摘要和目标摘要校验
    .then(isValid => {
        console.log(isValid);
    })
    .catch(error => console.error(error));

使用插件如出现Permission Denied,须检查文件是否可读取,以及是否开启文件读取权限。
插件在校验文件完成后会在本地目录下生成一个拓展名为算法名称的校验文件,存放此次校验文件的摘要值。

示例代码

部署插件后可使用如下代码(index.vue)进行调试,使用前须修改代码内的本地文件路径和本地校验文件路径。

<template>
    <view class="content">
        <view class="text-area">
            <text class="title">待校验本地文件:{{file}}</text>
        </view>
        <view class="text-area">
            <text class="title">网络校验文件:{{url}}</text>
        </view>
        <view class="text-area">
            <text class="title">本地校验文件:{{hashfile}}</text>
        </view>
        <view class="text-area">
            <text class="title">摘要算法:{{algorithm}}</text>
        </view>
        <view class="text-area">
            <text class="title">待验证的文件Hash:{{hash}}</text>
        </view>
        <button @click="verifyall">计算校验值</button>
        <view v-if="isshow">
            <view class="text-area">
                <text class="title">同步网络文件校验:<text :style="{color:isurl==true?'green':'red'}">{{isurl}}</text></text>
            </view>
            <view class="text-area">
                <text class="title">同步本地文件校验:<text :style="{color:isfile==true?'green':'red'}">{{isfile}}</text></text>
            </view>
            <view class="text-area">
                <text class="title">同步已知摘要校验:<text
                        :style="{color:isstring==true?'green':'red'}">{{isstring}}</text></text>
            </view>
            <view class="text-area">
                <text class="title">异步网络文件校验:<text
                        :style="{color:isurlasync==true?'green':'red'}">{{isurlasync}}</text></text>
            </view>
            <view class="text-area">
                <text class="title">异步本地文件校验:<text
                        :style="{color:isfileasync==true?'green':'red'}">{{isfileasync}}</text></text>
            </view>
            <view class="text-area">
                <text class="title">异步已知摘要校验:<text
                        :style="{color:isstringasync==true?'green':'red'}">{{isstringasync}}</text></text>
            </view>
        </view>
    </view>
</template>

<script>
    import {
        hashWithUrl, //1
        hashWithFile,
        hashWithString,
        hashWithUrlAsync,
        hashWithFileAsync,
        hashWithStringAsync
    } from '@/uni_modules/cfit-hashverify'
    export default {
        data() {
            return {
                file: 'file://你的文件路径,此例可先下载https://repo1.maven.org/maven2/log4j/log4j/1.2.9/maven-metadata.xml后调试',
                url: 'https://repo1.maven.org/maven2/log4j/log4j/1.2.9/maven-metadata.xml.sha1',
                hashfile: 'file://校验文件路径',
                algorithm: "sha1",
                hash: "41f582c44774800ea05547fc155cf63ce60d082e",
                isurl: false,
                isfile: false,
                isstring: false,
                isurlasync: false,
                isfileasync: false,
                isstringasync: false,
                isshow: false
            }
        },
        onLoad() {

        },
        methods: {
            verifyall() {
                this.isshow = true;
                this.isurl = (hashWithUrl(this.file, this.url, this.algorithm));
                console.log(this.isurl)
                this.isfile = (hashWithFile(this.file, this.hashfile, this.algorithm));
                console.log(this.isfile)
                this.isstring = (hashWithString(this.file, this.hash, this.algorithm));
                console.log(this.isstring)
                var that = this;
                hashWithUrlAsync(this.file, this.url, this.algorithm)
                    .then(isValid => {
                        console.log(isValid);
                        that.isurlasync = isValid;
                    })
                    .catch(error => console.error(error));
                hashWithFileAsync(this.file, this.hashfile, this.algorithm)
                    .then(isValid => {
                        console.log(isValid);
                        that.isfileasync = isValid;
                    })
                    .catch(error => console.error(error));
                hashWithStringAsync(this.file, this.hash, this.algorithm)
                    .then(isValid => {
                        console.log(isValid);
                        that.isstringasync = isValid;
                    })
                    .catch(error => console.error(error));
            }
        }
    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        padding: 10rpx;
        background-color: #f8f8f8;
        min-height: 100vh;
        box-sizing: border-box;
    }

    .text-area {
        width: 100%;
        margin-bottom: 24rpx;
        padding: 5rpx;
        background: white;
        border-radius: 12rpx;
        box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
    }

    .title {
        font-size: 32rpx;
        color: #333;
        line-height: 1.6;
        word-break: break-word;
    }

    .title text {
        font-weight: 500;
        margin-left: 10rpx;
    }

    button {
        width: 100%;
        background-color: #007AFF;
        color: white;
        font-size: 34rpx;
        border-radius: 50rpx;
        padding: 5rpx 0;
        margin: 5rpx 0;
        transition: all 0.3s;
    }

    button:active {
        background-color: #0062CC;
        transform: scale(0.98);
    }

    [style*="color:green"] {
        color: #09BE4F !important;
        font-weight: bold;
    }

    [style*="color:red"] {
        color: #FF5A5F !important;
        font-weight: bold;
    }

    .text-area:last-child {
        margin-bottom: 0;
    }
</style>

隐私、权限声明

1. 本插件需要申请的系统权限列表:

android.permission.WRITE_EXTERNAL_STORAGE(确保插件可读取目标文件)

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

暂无用户评论。

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问