(crypto.js)TypeError:数据必须是string或缓冲区

我目前使用crypto.js模块来哈希的东西。 这工作了一段时间,然后我开始得到这个错误:

在这里输入图像说明

这是我的服务器的基础:

process.stdout.write('\033c'); // Clear the console on startup var express = require("express"), app = express(), http = require("http").Server(app), io = require("socket.io")(http), path = require("path"), colorworks = require("colorworks").create(), fs = require("fs"), crypto = require("crypto"); function md5(msg){ return crypto.createHash("md5").update(msg).digest("base64"); } function sha256(msg) { return crypto.createHash("sha256").update(msg).digest("base64"); } http.listen(443, function(){ // Create the http server so it can be accessed via 127.0.0.1:443 in a web browser. console.log("NJ project webserver is running on port 443."); // Notify the console that the server is up and running }); app.use(express.static(__dirname + "/public")); app.get("/", function(request, response){ response.sendFile(__dirname + "/public/index.html"); }); 

我知道这些function正在产生问题:

  function md5(msg){ return crypto.createHash("md5").update(msg).digest("base64"); } function sha256(msg) { return crypto.createHash("sha256").update(msg).digest("base64"); } 

问题是,如果这些function不起作用(他们不再这么做),大约200行代码将会浪费。

这个错误是通过尝试散列一个不存在的variables来触发的:

 function md5(msg){ return crypto.createHash("md5").update(msg).digest("base64"); } function sha256(msg) { return crypto.createHash("sha256").update(msg).digest("base64"); } md5(non_existant); // This variable does not exist. 

你想要散列什么样的数据? 它从何而来 ? 我会先检查味精的价值,然后我会尝试:

 crypto.createHash('md5').update(msg.toString()).digest('hex'); 

您也可以使用这些包:

https://www.npmjs.com/package/md5

https://www.npmjs.com/package/js-sha256