1 package org.apache.fulcrum.jce.crypto;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.UnsupportedEncodingException;
23 import java.security.MessageDigest;
24 import java.security.NoSuchAlgorithmException;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class PasswordFactory implements PasswordParameters
40 {
41
42 private static PasswordFactory instance;
43
44 String algo;
45
46 int count = PasswordParameters.COUNT;
47
48 public PasswordFactory(String algo) {
49 this.algo = algo;
50 }
51
52 public PasswordFactory(String algo, int count) {
53 this.algo = algo;
54 this.count = count;
55 }
56
57
58
59
60
61 public synchronized static PasswordFactory getInstance()
62 {
63 if( PasswordFactory.instance == null )
64 {
65 PasswordFactory.instance = new PasswordFactory("SHA1");
66 }
67
68 return PasswordFactory.instance;
69 }
70
71
72
73
74
75
76
77 public synchronized static PasswordFactory getInstance(String algo)
78 {
79 if( PasswordFactory.instance == null )
80 {
81 PasswordFactory.instance = new PasswordFactory(algo);
82 }
83
84 return PasswordFactory.instance;
85 }
86
87
88
89
90
91
92
93
94 public synchronized static PasswordFactory getInstance(String algo, int count)
95 {
96 if( PasswordFactory.instance == null )
97 {
98 PasswordFactory.instance = new PasswordFactory(algo, count);
99 }
100
101 return PasswordFactory.instance;
102 }
103
104
105
106
107
108
109
110
111
112 public char[] create()
113 throws NoSuchAlgorithmException, UnsupportedEncodingException
114 {
115 return create(
116 PasswordParameters.DefaultPassword(),
117 PasswordParameters.Salt(),
118 count
119 );
120 }
121
122
123
124
125
126
127
128
129
130
131 public char[] create( String seed )
132 throws NoSuchAlgorithmException, UnsupportedEncodingException
133 {
134 return create(
135 seed.toCharArray()
136 );
137 }
138
139
140
141
142
143
144
145 public final char[] create( char[] seed )
146 throws NoSuchAlgorithmException, UnsupportedEncodingException
147 {
148 return create(
149 seed,
150 PasswordParameters.Salt(),
151 count
152 );
153 }
154
155
156
157
158
159
160
161
162
163
164
165 public char [] create( char[] password, byte[] salt, int count )
166 throws NoSuchAlgorithmException, UnsupportedEncodingException
167 {
168 char [] result = null;
169 MessageDigest sha1 = MessageDigest.getInstance( algo );
170 byte [] passwordMask = new String( password ).getBytes( "UTF-8" );
171 byte [] temp = new byte[salt.length + passwordMask.length];
172 byte [] digest = null;
173
174 StringBuilder stringBuffer = new StringBuilder();
175
176
177
178 System.arraycopy( passwordMask, 0, temp, 0, passwordMask.length );
179 System.arraycopy( salt, 0, temp, passwordMask.length, salt.length );
180
181
182
183 digest = temp;
184
185 for (int i = 0; i < count; i++)
186 {
187 sha1.update( digest );
188 digest = sha1.digest();
189 }
190
191
192
193
194 long long1 = createLong( digest, 0 );
195 long long2 = createLong( digest, 4 );
196 long long3 = createLong( digest, 8 );
197 long long4 = createLong( digest, 12 );
198
199 stringBuffer.append( Long.toHexString( long1 ).substring( 0, 4 ) );
200 stringBuffer.append( '-' );
201 stringBuffer.append( Long.toHexString( long2 ).substring( 0, 4 ) );
202 stringBuffer.append( '-' );
203 stringBuffer.append( Long.toHexString( long3 ).substring( 0, 4 ) );
204 stringBuffer.append( '-' );
205 stringBuffer.append( Long.toHexString( long4 ).substring( 0, 5 ) );
206
207
208 result = new char[stringBuffer.length()];
209
210 for (int i = 0; i < stringBuffer.length(); i++)
211 {
212 result[i] = stringBuffer.charAt( i );
213 }
214
215
216 for (int i = 0; i < stringBuffer.length(); i++)
217 {
218 stringBuffer.setCharAt( i, ' ' );
219 }
220
221 return result;
222 }
223
224
225
226
227
228
229
230
231 private static long createLong(byte [] buf, int nOfs)
232 {
233 return
234 ((long)(( buf[nOfs ] << 24) |
235 ((buf[nOfs + 1] & 0x0ff) << 16) |
236 ((buf[nOfs + 2] & 0x0ff) << 8) |
237 ( buf[nOfs + 3] & 0x0ff )) << 32) |
238 ((long)(( buf[nOfs + 4] << 24) |
239 ((buf[nOfs + 5] & 0x0ff) << 16) |
240 ((buf[nOfs + 6] & 0x0ff) << 8) |
241 ( buf[nOfs + 7] & 0x0ff )) & 0x0ffffffffL);
242 }
243 }