View Javadoc
1   package booking.model.util;
2   
3   import java.io.UnsupportedEncodingException;
4   import java.security.MessageDigest;
5   import java.security.NoSuchAlgorithmException;
6   import java.util.Formatter;
7   
8   public class SecurityUtil {
9   
10  	public static String encryptPassword(String password)
11  	{
12  	    String sha1 = "";
13  	    try
14  	    {
15  	        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
16  	        crypt.reset();
17  	        crypt.update(password.getBytes("UTF-8"));
18  	        sha1 = byteToHex(crypt.digest());
19  	    }
20  	    catch(NoSuchAlgorithmException e)
21  	    {
22  	        e.printStackTrace();
23  	    }
24  	    catch(UnsupportedEncodingException e)
25  	    {
26  	        e.printStackTrace();
27  	    }
28  	    return sha1;
29  	}
30  
31  	private static String byteToHex(final byte[] hash)
32  	{
33  	    Formatter formatter = new Formatter();
34  	    for (byte b : hash)
35  	    {
36  	        formatter.format("%02x", b);
37  	    }
38  	    String result = formatter.toString();
39  	    formatter.close();
40  	    return result;
41  	}
42  	
43  }