如何使用node.js加密来计算blob的sha1哈希值 [英] How to calculate the sha1 hash of a blob using node.js crypto

查看:281
本文介绍了如何使用node.js加密来计算blob的sha1哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试了以下方法: b $ b

  export函数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();
// });
}

从我的网站上选择带有文件上传按钮的文件。



如何计算sha1散列?

解决方案

if你正在阅读作为一个块的内容,你做得比它需要的更难。我们这样做:

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


In my node.js app I would like to upload a file and calculate the sha1 .

I tried the following :

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

The file is blob from selecting a file with a file upload button on my website.

How can I calculate the sha1 hash?

解决方案

if you're reading the contents in as a block, you're making this harder than it needs to be. We do this:

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

这篇关于如何使用node.js加密来计算blob的sha1哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆