5

This Javascript MD5 implementation has me confused.

In the global space, the author declares a var:

var hexcase = 0; 

Later on, the following method appears:

function rstr2hex(input)
{
  try { hexcase } catch(e) { hexcase=0; }
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var output = "";
  var x;
  for(var i = 0; i < input.length; i++)
  {
    x = input.charCodeAt(i);
    output += hex_tab.charAt((x >>> 4) & 0x0F)
           +  hex_tab.charAt( x        & 0x0F);
  }
  return output;
}

The line that I don't understand is:

try { hexcase } catch(e) { hexcase=0; }

What is the author trying to accomplish here?

1
  • +1 for mentioning my favourite JS crypto site :) Commented Nov 30, 2011 at 16:12

5 Answers 5

8

He is just making sure hexcase is defined, and if it isn't, he is defining it.

Try putting

try {amIdefined} catch(e) {console.log('was not defined');}

in your console and you'll see...

Note that this is the safest way of making sure the variable is defined. In order to do

hexcase = hexcase || 0;

you need to do var hexcase first, or else you will get an error.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Is: if (hexcase === undefined) { hexcase = 0; } not a better way of doing this? Google Closure complains about this statement (which seems to lack side-effects).
Global variable alert! Defining variables without "var" is like having sex without a condom. Comfortable, but you never know what you leave behind..
Not that I support this global variable idea in general, but an even better way to test for undefined is to write if (typeof hexcase == "undefined"). The variable undefined can be changed, but typeof cannot.
6

If hexcase does not exist, a ReferenceError is thrown, and the catch block is executed. If it does exist, the catch block is not executed.

So it sets hexcase to 0 if it does not exist.

It is a creative way of doing this, though. The more usual way is:

hexcase = window.hexcase || 0; // you have to add window because
                               // otherwise you would still get the error

5 Comments

Curious; why hexcase = hexcase || 0 and not hexcase === undefined?
i get a reference error if I do this without at least var hexcase;
@hvgotcodes: You're entirely correct, good catch. @ Steve: It's a bit cleaner, it reads as "existing value or otherwise 0".
@pimvdb This is when I wish I could mark more than one answer as accepted. Thanks for the help. Much appreciated.
@Steve that comparison will work only if hexcase has already been declared. A declared variable without assignment is undefined. But you can't compare an undefined variabled to anything, even if it is 'undefined'
2

Looks like hexcase is a global variable that the author is trying to check the existence of. Not sure that's the best way to do it though :-)

I'd go for:

if (typeof hexcase === "undefined") {
    hexcase = 0;
}

Just to make it explicit. You could use this too:

hexcase = hexcase || 0;

Comments

1

he's just checking to see if hexcase was defined, and if not sets a default.

apparantely that will decide whether the result is all uppercase or not...

Comments

0

That try-catch statement will set hexcase to 0 if it's undefined.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.