/*
 * Created on 2004/11/05
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.jware.util.crypt;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author jrmt
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PasswordUtils {

    /**
     * 
     */
    public PasswordUtils() {
        super();
    }

    /**
     * Encrypt password usin MD5 encryption algorithm
     * returns 32 char encoded String that is not recoverable
     */
    public static String encryptPassword( String orig ) {
        
        String uniqID = orig;
        
        try {
           
          MessageDigest sha = MessageDigest.getInstance("MD5");
          sha.update( orig.getBytes() );
          byte[] result =  sha.digest( orig.getBytes() );

          uniqID = hexEncode(result);

        }
        catch ( NoSuchAlgorithmException ex ) {
          System.err.println(ex);
        }
        
        return uniqID;
      }

    static private String hexEncode( byte[] aInput){
        StringBuffer result = new StringBuffer();
        char[] digits = {'0', '1', '2', '3', '4','5','6','7','8','9','A','B','C','D','E','F'};
        for ( int idx = 0; idx < aInput.length; ++idx) {
          byte b = aInput[idx];
          result.append( digits[ (b&0xf0) >> 4 ] );
          result.append( digits[ b&0x0f] );
        }
        return result.toString();
      }
    
}
