KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > util > PasswordEncryptionService


1 /*
2  * $Id: PasswordEncryptionService.java,v 1.2 2005/01/03 12:33:37 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.util;
27
28 import java.io.UnsupportedEncodingException JavaDoc;
29 import java.security.MessageDigest JavaDoc;
30 import java.security.NoSuchAlgorithmException JavaDoc;
31 import java.security.NoSuchProviderException JavaDoc;
32
33 import com.j2biz.blogunity.BlogunityManager;
34 import com.j2biz.blogunity.exception.BlogunityException;
35 import com.j2biz.blogunity.i18n.I18NStatusFactory;
36 import com.j2biz.blogunity.pojo.SystemConfiguration;
37 import com.j2biz.blogunity.pojo.User;
38
39 import sun.misc.BASE64Encoder;
40
41 public class PasswordEncryptionService {
42
43     public static final int MD5 = 1;
44
45     public static final int SHA = 2;
46
47     public static synchronized String JavaDoc encrypt(String JavaDoc text, int encryptionType)
48             throws NoSuchProviderException JavaDoc, NoSuchAlgorithmException JavaDoc,
49             UnsupportedEncodingException JavaDoc {
50
51         switch (encryptionType) {
52
53         case MD5:
54             return createMD5HashCode(text);
55
56         case SHA:
57             return createSHAHashCode(text);
58
59         default:
60             throw new NoSuchAlgorithmException JavaDoc(
61                     "Requested encription type ont found!");
62
63         }
64
65     }
66
67     /**
68      * Encrypts user's password if password encryption is activated in system
69      * settings.
70      *
71      * @param user
72      * @return
73      * @throws BlogunityException
74      */

75     public static synchronized User encryptPasswordIfNecessary(User user)
76             throws BlogunityException {
77         boolean isEncryptionEnbaled = false;
78         SystemConfiguration config = BlogunityManager.getSystemConfiguration();
79         if (config != null)
80             isEncryptionEnbaled = config.isPasswordEncriptionEnabled();
81
82         // encode password, if password-encryption is enabled!
83
if (isEncryptionEnbaled) {
84             try {
85                 String JavaDoc encodedPass = PasswordEncryptionService.encrypt(user
86                         .getPassword(), BlogunityManager
87                         .getSystemConfiguration().getPasswordEncryptionType());
88                 user.setPassword(encodedPass);
89             } catch (Throwable JavaDoc t) {
90                 throw new BlogunityException(I18NStatusFactory.createUnknown(t));
91             }
92         }
93
94         return user;
95     }
96
97     /**
98      * Get MD5 hashcode for a string.
99      *
100      * @param text
101      * Text to create the MD5 hash code for.
102      * @return MD5 hash code for <I>text </I> or the input text if failed.
103      * @throws NoSuchAlgorithmException
104      * @throws NoSuchProviderException
105      */

106     private synchronized static String JavaDoc createMD5HashCode(String JavaDoc text)
107             throws NoSuchAlgorithmException JavaDoc, NoSuchProviderException JavaDoc {
108         String JavaDoc result = text;
109
110         if (text != null) {
111             StringBuffer JavaDoc code = new StringBuffer JavaDoc(); //the hash code
112
MessageDigest JavaDoc messageDigest = MessageDigest.getInstance("MD5",
113                     "SUN");
114             String JavaDoc plain = text;
115             byte bytes[] = plain.getBytes();
116             byte digest[] = messageDigest.digest(bytes); //create code
117
for (int i = 0; i < digest.length; ++i) {
118                 code.append(Integer.toHexString(0x0100 + (digest[i] & 0x00FF))
119                         .substring(1));
120             }
121
122             result = code.toString();
123         }//else: input unavailable
124

125         return result;
126     }//createMD5HashCode()
127

128     /**
129      * Get SHA hashcode for a string.
130      *
131      * @param plaintext
132      * @return
133      * @throws NoSuchAlgorithmException
134      * @throws UnsupportedEncodingException
135      * @throws SystemUnavailableException
136      */

137     private synchronized static String JavaDoc createSHAHashCode(String JavaDoc plaintext)
138             throws NoSuchAlgorithmException JavaDoc, UnsupportedEncodingException JavaDoc {
139         MessageDigest JavaDoc md = null;
140
141         md = MessageDigest.getInstance("SHA"); //step 2
142

143         md.update(plaintext.getBytes("UTF-8")); //step 3
144

145         byte raw[] = md.digest(); //step 4
146
String JavaDoc hash = (new BASE64Encoder()).encode(raw); //step 5
147
return hash; //step 6
148
}
149
150 }
Popular Tags