Nanda Pandyatama
Nanda Pandyatama

Reputation: 101

CryptoJS cannot compare with pre-hashed data

i have a password table which containes SHA1 hashed data. so i want to make a validation input with CryptoJS. it went error.. but when i try to display it, the result matched.. did i code a wrong function?

here's my code

function checkCurPass()
{
    var hash = CryptoJS.SHA1("<?php echo $selected->password ?>");
    var currentPass = document.getElementById('users-profile-currentpassword');
    var hashedPass = CryptoJS.SHA1(currentPass.value);

    var message = document.getElementById('mesas');
    message.innerHTML = hashedPass;

    if (hashedPass == hash) 
    {
        $('#currentPassForm').addClass('has-success').removeClass('has-error');
    }
    else
    {
        $('#currentPassForm').addClass('has-error').removeClass('has-success');
    }
}

screenshot with hashed 'default'

enter image description here

Upvotes: 2

Views: 1803

Answers (1)

Sebastian S
Sebastian S

Reputation: 4712

Your hashes are no strings, but objects. If you console.log(hash), you'll see this. In order to get a proper hex string from these hashes, call hash.toString(CryptoJS.enc.Hex) and hashedPass.toString(CryptoJS.enc.Hex) respectively.

When you display your hash in your message element, toString is called implicitly, thats why the displayed strings were equal.

See http://codepen.io/anon/pen/BNxjGm

Upvotes: 3

Related Questions