gpt4 book ai didi

javascript - 如何使用 node.js 加密计算 blob 的 sha1 哈希

转载 作者:行者123 更新时间:2023-12-04 16:47:25 25 4
gpt4 key购买 nike

在我的 node.js 应用程序中,我想上传一个文件并计算 sha1。

我尝试了以下方法:

export function calculateHash(file, type){
const reader = new FileReader();
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
const testfile = reader.readAsDataURL(file);
hash.write(testfile);
hash.end();
var sha1sum = hash.read();
console.log(sha1sum);
// fd.on((end) => {
// hash.end();
// const test = hash.read();
// });
}

该文件是通过在我的网站上选择带有文件上传按钮的文件生成的 blob。

如何计算 sha1 哈希值?

最佳答案

如果您将其中的内容作为一个 block 来阅读,那么这将变得比需要的更难。我们这样做:

const fs = require('fs');
export function calculateHash(file, type){
const testfile = fs.readFileSync(file);
var sha1sum = crypto.createHash('sha1').update(testFile).digest("hex");
console.log(sha1sum);
}

关于javascript - 如何使用 node.js 加密计算 blob 的 sha1 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37849779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com