monkeytooth Posted March 30, 2011 Share Posted March 30, 2011 I have a user input string via GET variable that I want to always make sure falls within the constraints of type of variable its set for, which is a md5 hash. So I want to put together using preg_match or something using a regex construct (even though I suck with regex) to verify that all the characters in the string are valid characters for a md5 hash, where if its not then I want to kick back an error. The idea of making something like that isnt the issue for me so much as not knowing whether or not an md5 has is strictly alphanumeric or if it can possibly even if one in a million chance have something else from an underscore to a exclamation point or other characters. Ive tried to find out searching google and other sites for an answer but my search terms I guess are way to open to find something distinctly close to what I am looking for answer wise, so I figured I'd give you guys a shot see if anyone knows. Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/ Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 Q: md5 hashes are they only alphanumeric? A: Yes Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194694 Share on other sites More sharing options...
Pikachu2000 Posted March 30, 2011 Share Posted March 30, 2011 They're actually numeric. Hexadecimal to be precise, so they'll contain 0-9 and A-F. Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194700 Share on other sites More sharing options...
monkeytooth Posted March 30, 2011 Author Share Posted March 30, 2011 so if its 0-9 a-F whats the best way to handle the through regex, as I said before I'm kinda horrible with putting a regex statement together. Not worried about length so much of it as I am just making sure the string is a valid hash Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194710 Share on other sites More sharing options...
betterphp Posted March 30, 2011 Share Posted March 30, 2011 well they are chars, and we know the charset now. preg_match('#^[a-z0-9]{32}$#i', $_GET['something']); somethign like what should work I think. Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194714 Share on other sites More sharing options...
monkeytooth Posted March 30, 2011 Author Share Posted March 30, 2011 Cool thank you. By chance do you know any good regex resources that are kinda dumbed down for people to learn through? or just good in general I dont care. Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194716 Share on other sites More sharing options...
betterphp Posted March 31, 2011 Share Posted March 31, 2011 I learnt from here http://www.regular-expressions.info/ and this cheat sheet http://www.phpguru.org/downloads/PCRE%20Cheat%20Sheet/PHP%20PCRE%20Cheat%20Sheet.pdf and a bit form here too http://www.skdevelopment.com/php-regular-expressions.php Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194718 Share on other sites More sharing options...
monkeytooth Posted March 31, 2011 Author Share Posted March 31, 2011 Nice, thank you Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194727 Share on other sites More sharing options...
Zane Posted March 31, 2011 Share Posted March 31, 2011 well they are chars, and we know the charset now. preg_match('#^[a-z0-9]{32}$#i', $_GET['something']); somethign like what should work I think. Actually, since the letters only go to F, you don't need to look all the way to z... preg_match('#^[a-f0-9]{32}$#i', $_GET['something']); Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194740 Share on other sites More sharing options...
salathe Posted March 31, 2011 Share Posted March 31, 2011 You could also skip the regex and use plain old string functions. (strlen($blah) === 32 && ctype_xdigit($blah)) Quote Link to comment https://forums.phpfreaks.com/topic/232231-md5-hashes-are-they-only-alphanumeric/#findComment-1194820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.