Nicholas Smith
Nicholas Smith

Reputation: 720

(crypto.js) TypeError: Data must be string or buffer

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.

Upvotes: 4

Views: 13251

Answers (2)

Nicholas Smith
Nicholas Smith

Reputation: 720

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.

Upvotes: 5

fbhcf
fbhcf

Reputation: 111

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

Upvotes: 2

Related Questions