gpt4 book ai didi

javascript - 如何以 express 方式发送比较哈希?服务器到客户端

转载 作者:行者123 更新时间:2023-12-01 00:14:07 24 4
gpt4 key购买 nike

当客户端向服务器请求用于生成和授权访问文件的哈希值时,我需要回答客户端,如何使用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!");
});

最佳答案

您可以使用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)

})

关于javascript - 如何以 express 方式发送比较哈希?服务器到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59885993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com