在NodeJS中获取正确的Kekkak256上传文件

问题描述 投票:0回答:1

我正在使用keccak256 npm模块来计算上传文件的哈希值。我验证了正确性,我发现给定的散列与某些在线散列计算器(例如:thisthisthis)上的散列不对应。

我运行的代码是:

const express = require('express');
const fs = require('fs');
const keccak256 = require('keccak256');
const formidable = require('formidable'),
    http = require('http'),
    util = require('util');

const router = express.Router();

router.get('/', (req, res) => {
  res.sendFile('index.html', {root: __dirname })
});


router.post('/upload', (req, res) => {
  var form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload saved in: ' + files.upload.path + '\n\n');
      var readStream = fs.createReadStream(files.upload.path);
      //res.write(readStream);
      console.log(keccak256(readStream).toString('hex'))
      res.end("");

    });

    return;
});

任何人都可以向我解释我错在哪里以及如何纠正它?

javascript node.js hash keccak
1个回答
0
投票

使用NPM sha-3解决:

const express = require('express');
const fs = require('fs');
const formidable = require('formidable'),
    http = require('http'),
    util = require('util');
const { Keccak } = require('sha3');
const hash = new Keccak(256);

const router = express.Router();

router.get('/', (req, res) => {
  res.sendFile('index.html', {root: __dirname })
});


router.post('/upload', (req, res) => {
  var form = new formidable.IncomingForm();

    form.parse(req, function(err, fields, files) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload saved in: ' + files.upload.path + '\n\n');
      var file = fs.readFileSync(files.upload.path);
      hash.update(file);
      console.log(hash.digest('hex'));
      res.end("");

    });

    return;
});

现在它工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.