我正在使用keccak256 npm模块来计算上传文件的哈希值。我验证了正确性,我发现给定的散列与某些在线散列计算器(例如:this或this或this)上的散列不对应。
我运行的代码是:
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;
});
任何人都可以向我解释我错在哪里以及如何纠正它?
使用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;
});
现在它工作正常。