1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.jguard.core.util;
24
25 import java.security.MessageDigest;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.Security;
28 import java.util.Iterator;
29 import java.util.Set;
30 import java.util.concurrent.ArrayBlockingQueue;
31
32
33
34
35
36
37
38
39
40
41 public class CryptUtils
42 {
43
44 private final static int MAX_POOL_SIZE = 10;
45 private static ArrayBlockingQueue<MessageDigest> messageDigestPool = new ArrayBlockingQueue<MessageDigest>(MAX_POOL_SIZE);
46
47
48 public final static String NONE_ALGORITHM = "NONE";
49 private static String digestAlgorithm = NONE_ALGORITHM;
50
51 public static char[] salt = null;
52
53
54
55
56
57
58
59
60 private static char[] hexDump( byte src[] ) {
61 char buf[] = new char[src.length * 2];
62 for ( int b = 0; b < src.length; b++ ) {
63 String byt = Integer.toHexString( src[b] & 0xFF );
64 if ( byt.length() < 2 ) {
65 buf[b * 2 + 0] = '0';
66 buf[b * 2 + 1] = byt.charAt( 0 );
67 } else {
68 buf[b * 2 + 0] = byt.charAt( 0 );
69 buf[b * 2 + 1] = byt.charAt( 1 );
70 }
71 }
72 return buf;
73 }
74
75
76
77
78
79
80
81 public static void smudge( char pwd[] ) {
82 if ( null != pwd ) {
83 for ( int b = 0; b < pwd.length; b++ ) {
84 pwd[b] = 0;
85 }
86 }
87 }
88
89
90
91
92
93
94 public static void smudge( byte pwd[] ) {
95 if ( null != pwd ) {
96 for ( int b = 0; b < pwd.length; b++ ) {
97 pwd[b] = 0;
98 }
99 }
100 }
101
102
103
104
105
106
107
108
109
110 public static char[] cryptPassword( char[] pwd )
111 throws NoSuchAlgorithmException
112 {
113
114 char[] newPwd = null;
115
116 if ( salt != null ) {
117 newPwd = saltPassword( pwd );
118 } else {
119 newPwd = pwd;
120 }
121
122
123 if ( digestAlgorithm.equals( NONE_ALGORITHM ) ) {
124 return newPwd;
125 }
126
127
128 MessageDigest messageDigest = null;
129 char crypt[] = null;
130 try {
131 messageDigest = messageDigestPool.take();
132 messageDigest.reset();
133
134
135 byte pwdb[] = new byte[newPwd.length];
136
137 for ( int b = 0; b < newPwd.length; b++ ) {
138 pwdb[b] = (byte)newPwd[b];
139 }
140
141
142 crypt = hexDump( messageDigest.digest( pwdb ) );
143 smudge( pwdb );
144 } catch ( InterruptedException ex ) {
145
146 throw new IllegalStateException( ex );
147 } finally {
148 messageDigestPool.offer(messageDigest);
149 }
150
151 return crypt;
152 }
153
154 private static char[] saltPassword( char[] pwd ) {
155
156 char[] merged = new char[pwd.length + salt.length];
157 int mergedIndex = 0;
158 for ( int i = 0; i < pwd.length; i++ ) {
159 merged[mergedIndex] = pwd[i];
160 mergedIndex++;
161 }
162 for ( int j = 0; j < salt.length; j++ ) {
163 merged[mergedIndex] = salt[j];
164 mergedIndex++;
165 }
166 return merged;
167 }
168
169
170
171
172 public static String getDigestAlgorithm() {
173 return digestAlgorithm;
174 }
175
176
177
178
179
180
181
182 public static void setDigestAlgorithm(String algorithm)
183 throws NoSuchAlgorithmException
184 {
185 if ( NONE_ALGORITHM.equals( algorithm ) ) {
186 digestAlgorithm = algorithm;
187 return;
188 }
189 Set algorithmsSet = Security.getAlgorithms( "MessageDigest" );
190 if ( algorithmsSet.size() < 1 ) {
191 throw new NoSuchAlgorithmException( "no Message Digest algorithms implemented in this jvm " );
192 }
193 Iterator it = algorithmsSet.iterator();
194 boolean algorithmImplemented = false;
195 String algorithmTemp;
196
197 short count = 0;
198 while ( it.hasNext() ) {
199 count++;
200 algorithmTemp = (String)it.next();
201 if ( algorithmTemp.equalsIgnoreCase( algorithm ) ) {
202 algorithmImplemented = true;
203 break;
204 }
205 }
206
207 if ( algorithmImplemented == true ) {
208 digestAlgorithm = algorithm;
209 } else {
210 throw new NoSuchAlgorithmException( "Message Digest algorithm '" + algorithm + "' not implemented " );
211 }
212 initMessageDigestPool();
213 }
214
215 private static void initMessageDigestPool()
216 throws NoSuchAlgorithmException
217 {
218 for ( int i = 0; i < MAX_POOL_SIZE; i++ ) {
219 messageDigestPool.add( MessageDigest.getInstance(digestAlgorithm));
220 }
221 }
222
223
224
225
226
227
228
229
230 public static boolean setSalt( char[] saltCandidate ) {
231 if ( salt != null && saltCandidate != null && !saltCandidate.equals( "" ) ) {
232 return false;
233 }
234 CryptUtils.salt = saltCandidate;
235 return true;
236
237 }
238
239 public static char[] getSalt() {
240 return salt;
241 }
242
243 }