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.IOException;
23 import java.io.InputStream;
24 import java.security.GeneralSecurityException;
25 import java.security.Key;
26
27 import javax.crypto.Cipher;
28 import javax.crypto.SecretKeyFactory;
29 import javax.crypto.spec.PBEKeySpec;
30 import javax.crypto.spec.PBEParameterSpec;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class CryptoStreamFactoryImpl extends CryptoStreamFactoryTemplate implements CryptoStreamFactory
53 {
54
55 protected final byte[] salt;
56
57
58 protected int count;
59
60
61 protected String providerName;
62
63
64 protected String algorithm;
65
66
67 private static CryptoStreamFactory instance;
68
69
70
71
72
73
74 protected static final String PROVIDERNAME = null;
75
76
77
78
79
80 public static CryptoStreamFactory getInstance()
81 {
82 synchronized(CryptoStreamFactoryImpl.class ) {
83 if( CryptoStreamFactoryImpl.instance == null )
84 {
85 CryptoStreamFactoryImpl.instance = new CryptoStreamFactoryImpl();
86 }
87
88 return CryptoStreamFactoryImpl.instance;
89 }
90 }
91
92
93
94
95
96 public static void setInstance( CryptoStreamFactory instance )
97 {
98 CryptoStreamFactoryImpl.instance = instance;
99 }
100
101
102
103
104 public CryptoStreamFactoryImpl()
105 {
106 this.salt = CryptoParameters.Salt();
107 this.count = CryptoParameters.COUNT;
108 this.providerName = PROVIDERNAME;
109 this.algorithm = CryptoParameters.ALGORITHM;
110 }
111
112
113
114
115
116
117
118 public CryptoStreamFactoryImpl( byte[] salt, int count)
119 {
120 this.salt = salt.clone();
121 this.count = count;
122 this.providerName = PROVIDERNAME;
123 this.algorithm = CryptoParameters.ALGORITHM;
124 }
125
126
127
128
129
130
131
132
133
134
135 public static CryptoStreamFactory getInstance( byte[] salt, int count)
136 {
137 synchronized(CryptoStreamFactoryImpl.class ) {
138 if( CryptoStreamFactoryImpl.instance == null )
139 {
140 CryptoStreamFactoryImpl.instance = new CryptoStreamFactoryImpl(salt, count);
141 }
142
143 return CryptoStreamFactoryImpl.instance;
144 }
145 }
146
147
148
149
150
151
152 public InputStream getSmartInputStream(InputStream is, char[] password )
153 throws GeneralSecurityException, IOException
154 {
155 SmartDecryptingInputStream result;
156
157 result = new SmartDecryptingInputStream(
158 getInstance(),
159 is,
160 password
161 );
162
163 return result;
164 }
165
166
167
168
169 @Override
170 public String getAlgorithm()
171 {
172 return algorithm;
173 }
174
175
176
177
178 protected int getCount()
179 {
180 return count;
181 }
182
183
184
185
186 protected String getProviderName()
187 {
188 return providerName;
189 }
190
191
192
193
194 protected byte [] getSalt()
195 {
196 return salt;
197 }
198
199
200
201
202
203
204
205
206 protected Key createKey( char[] password )
207 throws GeneralSecurityException
208 {
209 SecretKeyFactory keyFactory;
210 String algorithm = this.getAlgorithm();
211 PBEKeySpec keySpec = new PBEKeySpec(password);
212
213 if( this.getProviderName() == null )
214 {
215 keyFactory = SecretKeyFactory.getInstance( algorithm );
216 }
217 else
218 {
219 keyFactory = SecretKeyFactory.getInstance( algorithm, this.getProviderName() );
220 }
221
222 return keyFactory.generateSecret(keySpec);
223 }
224
225
226
227
228
229
230
231
232
233
234 protected Cipher createCipher( int mode, char[] password )
235 throws GeneralSecurityException, IOException
236 {
237 Cipher cipher;
238 PBEParameterSpec paramSpec = new PBEParameterSpec( this.getSalt(), this.getCount() );
239 Key key = this.createKey( password );
240
241 if( this.getProviderName() == null )
242 {
243 cipher = Cipher.getInstance( this.getAlgorithm() );
244 }
245 else
246 {
247 cipher = Cipher.getInstance( this.getAlgorithm(), this.getProviderName() );
248 }
249
250 cipher.init( mode, key, paramSpec );
251 return cipher;
252 }
253 }