【问题标题】:How I send a compare a hash in express? server to client我如何发送比较散列快递?服务器到客户端
【发布时间】:2020-05-10 03:56:12
【问题描述】:

当客户端要求服务器生成哈希并授权访问文件时,我需要回答,我如何使用 node.js 和 express 确认哈希等于消息:

const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const sha1 = require('sha1');
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function(request, response) {
  var x1 = request.query.x1; 
  var x2 = request.query.x2; 
  var x3 = request.query.x3; 
  var x4 = 654321; 
  var hash = sha1(x2+x3+x4)
});
app.listen(process.env.PORT || 3000, function() {
  console.log("Server is running in port 3000!");
});

*

【问题讨论】:

    标签: javascript node.js express server client


    【解决方案1】:

    您可以使用crypto 模块来生成和解码哈希。这是一个例子。

    const resizedIV = Buffer.allocUnsafe(16)
    app.get('/', function(request, response) {
    
        const key = crypto
          .createHash("sha256")
          .update('secret-key')//this should be a secret key
          .digest()
    
       const cipher = crypto.createCipheriv("aes256", key, resizedIV)
    
       for (var prop in req.query) {
        if (req.query.hasOwnProperty(prop)) {
        cipher.update(req.query[prop], "binary", "hex")
        }
       }
        const hash = cipher.final("hex")
        res.send(hash)
    });
    

    然后你就可以这样解码了

    app.get('/decode', function(req, res) {
        const key = crypto
            .createHash("sha256")
            .update('secret-key')
            .digest()
    
         const decipher = crypto.createDecipheriv("aes256", key, resizedIV),
    
    
            decipher.update(req.query.hash, "hex", "binary")
    
    
           const decoded = decipher.final("binary")
    
           res.send(decoded)
    
    })
    

    【讨论】:

      猜你喜欢
      • 2019-05-27
      • 2021-04-27
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多