MD5 Java 模式
我正在关注 BalusC 的 DAO 教程,其中有这个函数:
private static String hashMD5IfNecessary(String password) {
return !"^[a-f0-9]{32}$".matches(password) ? hashMD5(password) : password;
}
我与之耦合:
<h:inputText value="#{myBean.password}" />
但是 "^[a-f0-9]{32}$".matches(password)
(其中 password
已从MySQL 表)始终返回 false
,即使传递的是 MD5 哈希密码(例如 21232f297a57a5a743894a0e4a801fc3
)。
我还尝试了以下模式:
[a-f0-9]{32}
[a-f0-9]{32}+
但它们仍然总是评估为假
。另外,我非常怀疑BalusC的原始代码是错误的。我做错了什么?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅http://download.oracle .com/javase/6/docs/api/java/lang/String.html#matches(java.lang.String)
matches() 方法采用正则表达式作为参数,所以鉴于你在问题中所写的内容,它会始终返回 false,因为密码不太可能是与“^[a-f0-9]{32}$”匹配的正则表达式。
尝试
一下
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
instead