【问题标题】:How to calculate the sha1 hash of a blob using node.js crypto如何使用 node.js 加密计算 blob 的 sha1 哈希
【发布时间】:2016-10-17 09:58:01
【问题描述】:

在我的 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 哈希?

【问题讨论】:

  • CryptoJS 主要是一个客户端库,不应与 node.js 的加密模块混淆。哪一款适合你?
  • 您在此处提供的代码有什么问题?它不是已经工作了吗?

标签: javascript node.js hash sha1


【解决方案1】:

如果您将其中的内容作为一个块来阅读,那么您的阅读难度就会超出预期。我们这样做:

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);
}

【讨论】:

  • 解决方案产生错误。无法读取未定义的属性“长度”
  • testfiletestfiletestFile 的拼写修复后对我来说很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
  • 2011-04-30
相关资源
最近更新 更多