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

时间:2019-02-13 22:00:38

标签: javascript node.js hash keccak

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

我运行的代码是:

// import * as AWS from 'aws-sdk';

exports.handler = async(event) => 
{
    const cloudfront = new AWS.CloudFront();

    const distID = process.env.DISTRIBUTION_ID;
    const path = (event.queryStringParameters.path);
    console.log(event.queryStringParameters.path);

    const params = 
    {
        DistributionId: distID, 
        InvalidationBatch: 
        { 
            CallerReference: (event.requestContext.requestId), 
            Paths: { Quantity: 1,  Items: [ path ] }
        }
    };

    try
    {
        const resp = await cloudfront.createInvalidation(params).promise();

        console.log(resp);
    }
    catch (ex)
    {
        console.error(ex);
    }
};

任何人都可以向我解释我哪里错了,我该如何纠正?

1 个答案:

答案 0 :(得分: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;
});

现在可以正常工作了。