-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: How to save user password with MD5 to MySQL 4?
PostPosted: Sat Oct 30, 2004 12:01 pm 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Hibernate version:2.1.6

Mapping documents:
<property
name="password"
type="java.lang.String"
column="PASSWD"
/>


Code between sessionFactory.openSession() and session.close():
public void saveUser(final User user) {
this.getHibernateTemplate().save(user);
}


Name and version of the database you are using:MySql 4.0.21

Hi guys,

Anyone can show me how to save user password iwth MD5 encryption? As I just save user data using
this.getHibernateTemplate().save(user);

regards,
prettyhandling


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 30, 2004 6:57 pm 
Newbie

Joined: Sat Oct 30, 2004 3:48 pm
Posts: 14
Just create a UserType (http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-types-custom)

In your method:
Code:
nullSafeSet(PreparedStatement st, Object value, int index)

Generate the md5 for the password


Top
 Profile  
 
 Post subject: Any example?
PostPosted: Sun Oct 31, 2004 2:18 am 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
dwsmith75 wrote:
Just create a UserType (http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-types-custom)

In your method:
Code:
nullSafeSet(PreparedStatement st, Object value, int index)

Generate the md5 for the password


Hi,

any example? let say if I got a users table contain the following columns:

userid, passwd, email.

The usual method is just like

Users users = new Users();
users.setUserid("tomcat");
users.setPasswd("tomcat");
users.setEmail("tom@tom.com");
usersDao.save(users);

how to implement the
nullSafeSet(PreparedStatement st, Object value, int index) ?

Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 31, 2004 1:37 pm 
Newbie

Joined: Sat Oct 30, 2004 3:48 pm
Posts: 14
Mapping documents:
<property
name="password"
type="blah.MD5sumType"
column="PASSWD"
/>

Code:
package blah;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

public class MD5sumType implements UserType {

    public int[] sqlTypes() {
        return new int[] { Types.VARCHAR };
       
    }

    public Class returnedClass() {
        return String.class;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        return (x == y) || (x != null && y != null && (x.equals(y)));
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        String val = rs.getString(names[0]);
        return val != null ? val.trim() : "";
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        String passwd = (String)value;
          // **********************************
         // generate md5sum for passwd
         // ************************************
        st.setString(index, passwd);
    }

    public Object deepCopy(Object value) throws HibernateException {
        if (value == null) return null;

        return new String((String)value);
    }

    public boolean isMutable() {
        return true;
    }
}


Top
 Profile  
 
 Post subject: Passwd remain the same !
PostPosted: Sun Oct 31, 2004 10:38 pm 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Hi dwsmith75,

I tried

public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
String passwd = null;
passwd = (String)value;
passwd = CrytoGenerator.generateDesCryto(passwd);
st.setString(index, passwd);
}

the CrytoGenerator.generateDesCryto(passwd) will return an encrypted String.

==============================
/**
* @return Returns the password.
* @hibernate.property column="PASSWD"
* not-null=


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 31, 2004 11:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please read the postings of the people answering you:


dwsmith75 wrote:
Mapping documents:
<property
name="password"
type="blah.MD5sumType"
column="PASSWD"
/>

You can NOT keep the mapping file the same.


Top
 Profile  
 
 Post subject: Re: Passwd remain the same !
PostPosted: Sun Oct 31, 2004 11:03 pm 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Hi,

Usually we will just use the sql statement like
insert into user values('person',password('mypassword'));

then mysql will store the password column as MD5sum. How to do this with Hibernate?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.