Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is an MD5 Hash

Tags:

php

hash

md5

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?

like image 646
kmoney12 Avatar asked Jan 13 '13 04:01

kmoney12


People also ask

How do I check MD5 format?

Open a terminal window. Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file.


2 Answers

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.
like image 125
NullPoiиteя Avatar answered Sep 20 '22 13:09

NullPoiиteя


Maybe a bit faster one:

function isValidMd5($md5 ='') {   return strlen($md5) == 32 && ctype_xdigit($md5); } 
like image 39
RaphaelH Avatar answered Sep 22 '22 13:09

RaphaelH