Understanding Changes in Magento Password Hashing
As Magento evolves through its iterations, it introduces updates and security enhancements that can impact how password hashing is handled. In your case, using Magento version 1.9.0.1, you’ve noticed a discrepancy when attempting to validate passwords outside the standard framework login process.
The Original Password Hashing Mechanism
In earlier versions of Magento like 1.9.0.1, the customer_entity_varchar table stored hashed and salted passwords for customer authentication. The format was a colon-separated string containing the hash type followed by the actual hash value and then the salt. Your proof-of-concept function is attempting to parse this format correctly:
function checkPassword($entity,$passwordInput){
$query = mysql_query("SELECT value FROM customer_entity_varchar WHERE entity_id = '$entity' AND attribute_id = '12' LIMIT 1");
$fetch = mysql_fetch_object($query);
$fetch_data = explode(':',$fetch->value);
$hashed_password = $fetch_data['0'];
$salt = $fetch_data['1'];
$hashInput = md5($passwordInput . $salt);
if($hashInput == $hashed_password){
return 'Success';
}
else{
return 'Failure';
}
}
The Shift to SHA256 Hashing
Starting from Magento 1.7, the core password hashing algorithm was changed from MD5 to SHA256 for enhanced security. This change is significant because it affects how you should handle and validate passwords outside of the built-in login system.
In your specific scenario with version 1.9.0.1, the customer_entity_varchar attribute ID ‘12’ corresponds to the password_hash field. However, the hashing method used (SHA256) is different from what your function expects (MD5).
Verifying and Updating Your Function
To resolve this issue, you need to update your password validation logic to account for the SHA256 hashing:
function checkPassword($entity,$passwordInput){
$query = mysql_query("SELECT value FROM customer_entity_varchar WHERE entity_id = '$entity' AND attribute_id = '12' LIMIT 1");
$fetch = mysql_fetch_object($query);
$hashed_password = hash('sha256', $passwordInput . $fetch->value);
if(hash_equals($hashed_password, $fetch->value)){
return 'Success';
}
else{
return 'Failure';
}
}
The hash('sha256', ...) function is used to generate a SHA256 hash of the input password concatenated with the stored salt. The hash_equals function then compares this generated hash with the stored value in an optimized and secure manner.
Conclusion
In summary, Magento has indeed updated its password hashing mechanism over time to improve security standards. If you’re working with older versions like 1.9.0.1 outside of the standard login process, ensure your custom validation functions are adapted to the current hashing algorithm in use (SHA256 in this case).
Remember, security is a top priority when handling user credentials, and it’s crucial to stay updated with the latest best practices as you develop your applications.
For more detailed information on Magento’s password handling and security features, consider exploring our comprehensive guides at Laravel Company.