4

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

enter image description here

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.

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

2 Answers 2

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.
2

What kind of data are you trying to hash ? Where does it come 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.com/package/md5

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

2
  • I am trying to hash text Commented Dec 19, 2016 at 12:19
  • can you check what value msg has before getting hashed ? like console.log(msg)
    – fbhcf
    Commented Dec 19, 2016 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.