var secure;
var authentic;
secure = prompt("Enter password","");
alert(secure);
var c1 = CryptoJS.SHA256(secure);
alert(c1);
authentic = prompt("Enter password","");
alert(authentic);
var c2=CryptoJS.SHA256(authentic);
alert(c2);
if(c1==c2)
{
alert("hi");
}
else
{
alert("bye");
}
I use the script "http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"
EXPLANATION: What, I'm trying to do is to compare hashed passwords.If both the passwords entered (secure and authentic) that have been hashed (c1 and c2 respectively) are equal, It must display 'hi' to me. But I find that it displays 'bye' to me always.
PROBLEM: When I compare c1 and c2, the result I get always is 'bye', though the values of c1 and c2 are the same when I display them using the alert box.
I'm kinda new to hashing. A li'l help would be much appreciated!
From the docs:
The hash you get back isn't a string yet. It's a WordArray object. When you use a WordArray object in a string context, it's automatically converted to a hex string.
When you compare objects in JavaScript you are testing to see if they are the same object, not if they are identical objects.
Since you have created two WordArrays, you are comparing two different (but probably identical) objects.
You need to convert them to strings.
if ( (''+c1) == (''+c2) )
or
if ( c1.toString() == c2.toString() )