0

我正在使用 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;
});

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

4

1 回答 1

0

通过使用NPMsha-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;
});

现在它工作正常。

于 2019-02-14T07:48:12.023 回答