Sunday, 6 August 2017

php - Check if string is an MD5 Hash



I accidentally stopped hashing passwords before they were stored, so now my database has a mix of MD5 Passwords and unhashed passwords.



I want to loop through and hash the ones that are not MD5. Is it possible to check if a string is an MD5 hash?


Answer



You can check using the following function:




function isValidMd5($md5 ='')
{
return preg_match('/^[a-f0-9]{32}$/', $md5);
}

echo isValidMd5('5d41402abc4b2a76b9719d911017c592');


The MD5 (Message-digest algorithm) Hash is typically expressed in text format as a 32 digit hexadecimal number.




This function checks that:




  1. It contains only letters and digits (a-f, 0-9).

  2. It's 32 characters long.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...