petebolduc
petebolduc

Reputation: 1263

How to get a consistent return using hash_hmac()

I am getting a different password key returned with hash_hmac() for the key generated at registration and the key returned at login using the exact same code.

The code use at registration is:

$hash_key = 'mL993nHbLJe8dOeC242A8W'; 
    $hmac = hash_hmac('sha256', $pass, $hash_key); 
    $hmac = substr($hmac, 0, 50);

and the code used to create the key to compare to is:

$hash_key = 'mL993nHbLJe8dOeC242A8W'; 
    $hmac = hash_hmac('sha256', $pass, $hash_key); 
    $hmac = substr($hmac, 0, 50);   

Here is what is being returned:

53bccd32d23baf691bf9c0a01b0deaa079107a72b72f4a4c08 --- Pass Key Generated by Login
4c30997095ad3f061246ff22d096d3537f1d9d22653f533653 --- Pass Key Retrieved which was Generated at Registration

I am using the substr($hmac, 0, 50); which varies with each registration but always returns a 50 count. Not sure if this is a good idea or not, perspectives are welcomed, but the same issue occurs with or without the substr($hmac, 0, 50);.

I have also tried sha512 with the same issue.

The above code worked for me on wamp on my computer but when uploaded to a share host the above mention problem occurred. Assitance is appreciated,

Pete

Upvotes: 2

Views: 582

Answers (1)

petebolduc
petebolduc

Reputation: 1263

ISSUE RESOLVED...

After an all day struggle with this issue it was resolved with wrapping $pass in a trim($pass) php function. Go figure. Thanks for all who took the time to assist with input.

Pete

Upvotes: 2

Related Questions