View Javadoc

1   /*
2    * Created on Nov 18, 2004
3    *
4    */
5   package org.musicontroller.security;
6   
7   import java.security.MessageDigest;
8   import java.security.NoSuchAlgorithmException;
9   
10  /**
11   * The Encrypter is used to encrypt passwords of Users.
12   * @author Varienaja
13   * @version $Id: Encrypter.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
14   */
15  public class Encrypter {
16  
17  	/**
18  	 * Encryptd the incoming String using the SHA messsage digester.
19  	 * @param unencrypted The unencrypted string.
20  	 * @return The encrypted string.
21  	 */
22  	public static String encrypt(String unencrypted) {
23  		if (unencrypted==null) { unencrypted=""; };
24  		MessageDigest md;
25  		try {
26  			md = MessageDigest.getInstance("SHA");
27  		} catch (NoSuchAlgorithmException e) {
28  			return e.getLocalizedMessage();
29  		}
30  		byte[] bytes = md.digest(unencrypted.getBytes());
31  		StringBuffer hexString = new StringBuffer();
32  		for (int i=0;i<bytes.length;i++) {
33  			String hexdigit =Integer.toHexString(0xFF & bytes[i]);
34  			if (hexdigit.length()==1) {
35  				hexdigit = '0' + hexdigit;
36  			}
37  			hexString.append(hexdigit);
38  	    }
39  		return hexString.toString();
40  	}
41  	
42  }