简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM