admin管理员组

文章数量:1289483

I am currently using crypto.js module to hash things. It was working for a while then I started getting this error:

Here is the foundation of my server:

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");
        });

I am aware that these functions are creating the problem:

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

The problem being, if these functions don't work (which they don't anymore), roughly 200 lines of code will go to waste.

I am currently using crypto.js module to hash things. It was working for a while then I started getting this error:

Here is the foundation of my server:

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");
        });

I am aware that these functions are creating the problem:

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

The problem being, if these functions don't work (which they don't anymore), roughly 200 lines of code will go to waste.

Share Improve this question edited Dec 19, 2016 at 14:40 Nicholas Smith asked Dec 19, 2016 at 9:23 Nicholas SmithNicholas Smith 7202 gold badges13 silver badges27 bronze badges 3
  • Where do you use this functions? – Ron Dadon Commented Dec 19, 2016 at 9:37
  • @RonDadon I call the functions in various areas of my server (usually when sockets are received though). Here is an example: var usernameEncryption = sha256(md5(salt)); – Nicholas Smith Commented Dec 19, 2016 at 9:53
  • 1 Try to run it in debug and place a break point in the function to check the data that is passed via the msg argument, because in at least one call you are not passing a string (or a buffer). – Ron Dadon Commented Dec 19, 2016 at 12:39
Add a ment  | 

2 Answers 2

Reset to default 5

This error is triggered by attempting to hash a variable that does not exist:

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

What kind of data are you trying to hash ? Where does it e from ? I would check the value of msg first then I would try :

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

You could also use these packages instead:

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

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

本文标签: javascript(cryptojs) TypeError Data must be string or bufferStack Overflow