MD5 Java Pattern
I am following BalusC's DAO Tutorial, where there is this function:
private static String hashMD5IfNecessary(String password) {
return !"^[a-f0-9]{32}$".matches(password) ? hashMD5(password) : password;
}
which I开发者_开发知识库 coupled with:
<h:inputText value="#{myBean.password}" />
But "^[a-f0-9]{32}$".matches(password)
(where password
has been retrieved from a MySQL table) always returns false
, even when it is passed an MD5-hashed password, like 21232f297a57a5a743894a0e4a801fc3
.
I've also tried the following patterns:
[a-f0-9]{32}
[a-f0-9]{32}+
but they still always evaluate to false
. Besides, I highly doubt that BalusC's original code is wrong. What am I doing wrong?
Thanks!
see http://download.oracle.com/javase/6/docs/api/java/lang/String.html#matches(java.lang.String)
the matches() method takes a regex as the parameter, so given what you've written in the question, it will always return false, as the password is unlikely to be a regex that matches "^[a-f0-9]{32}$".
Try
password.matches("^[a-f0-9]{32}$")
instead
精彩评论