开发者

MD5 and Hibernate Query

public Login authenticate(Login login) {
         String query = "SELECT L FROM Login AS L WHERE L.email=? AND L.password=?";
         Object[] parameters = { login.getEmail(), login.getPassword() };
         List<Login> resultsList = 开发者_JAVA技巧(getHibernateTemplate().find(query,parameters));
         if (resultsList.isEmpty()) {
             //error dude
         }
         else if (resultsList.size() > 1) {
             //throw expections
         }
         else {
           Login login1 = (Login) resultsList.get(0);
           return login1;
         }       
       return null;  
    }

I have my DB tables password col set as MD5, now how to retrieve it back here.


You'll have to hash the password and pass the hash as a parameter. Some thing like:

String hash = hash(login.getPassword());
Object[] parameters = { login.getEmail(), hash };

For how to implement the hash(..) method, see this question. However, avoid MD5. Use SHA instead.


I beleive you would want to convert your L.password to md5 before calling the authenticate.

See this useful link

import java.security.*;

..

byte[] bytesOfMessage = yourString.getBytes("UTF-8");

MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜