user Password Hash - Java Security

Java examples for Security:Password

Description

user Password Hash

Demo Code


import java.security.MessageDigest;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        String password = "java2s.com";
        String username = "java2s.com";
        System.out.println(userPasswordHash(password,username));
    }/*ww  w  . j  a  v a 2 s  .com*/
    private static final String USER_PASSWORD_SALT = "hotento";
    private static final Logger log = Logger
            .getLogger(AuthenticationHelper.class);
    public static String userPasswordHash(String password, String username) {
        String hash = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(USER_PASSWORD_SALT.getBytes("UTF-8"));
            md.update(username.getBytes("UTF-8"));
            byte[] byteHash = md.digest(password.getBytes("UTF-8"));
            hash = new String(byteHash, "UTF-8");
        } catch (Exception ex) {
            log.fatal(ex.getMessage(), ex);
        }
        return hash;
    }
}

Related Tutorials