php 检查字符串是否为 MD5 哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14300696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if string is an MD5 Hash
提问by kmoney12
I accidentally stopped hashing passwords before they were stored, so now my database has a mix of MD5 Passwords and unhashed passwords.
我不小心在存储密码之前停止了散列,所以现在我的数据库混合了 MD5 密码和未散列的密码。
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?
我想遍历并散列不是 MD5 的那些。是否可以检查字符串是否是 MD5 哈希?
回答by NullPoiиteя
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.
MD5(消息摘要算法)哈希通常以文本格式表示为 32 位十六进制数。
This function checks that:
此函数检查:
- It contains only letters and digits (a-f, 0-9).
- It's 32 characters long.
- 它只包含字母和数字(af,0-9)。
- 它的长度为 32 个字符。
回答by RaphaelH
Maybe a bit faster one:
也许更快一点:
function isValidMd5($md5 ='') {
return strlen($md5) == 32 && ctype_xdigit($md5);
}

