1 package org.apache.fulcrum.jce.crypto.extended;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.security.GeneralSecurityException;
27 import java.security.Key;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.SecureRandom;
30 import java.util.Map;
31 import java.util.concurrent.ConcurrentHashMap;
32
33 import javax.crypto.Cipher;
34
35 import org.apache.fulcrum.jce.crypto.PasswordFactory;
36 import org.apache.fulcrum.jce.crypto.StreamUtil;
37 import org.apache.fulcrum.jce.crypto.algo.CryptoStreamGCMImpl;
38 import org.apache.fulcrum.jce.crypto.algo.CryptoStreamPBEImpl;
39 import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
40
41
42
43
44
45
46 public abstract class CryptoStreamFactoryJ8Template implements CryptoStreamFactoryJ8
47 {
48
49
50 protected byte[] salt;
51
52
53 protected int count;
54
55
56 protected String providerName;
57
58
59 protected String algorithm;
60
61 private TYPES type;
62
63 public TYPES getType() {
64 return type;
65 }
66
67 public void setType(TYPES type) {
68 this.type = type;
69 }
70
71
72
73
74
75
76 protected static final String PROVIDERNAME = null;
77
78 protected static final int SALT_SIZE = 16;
79
80
81 protected static final Map<TYPES,CryptoStreamFactoryJ8Template> instances = new ConcurrentHashMap<>();
82
83
84
85 public CryptoStreamFactoryJ8Template() {
86
87 }
88
89
90
91
92
93
94
95
96 public static CryptoStreamFactoryJ8 getInstance()
97 {
98 synchronized (CryptoStreamFactoryJ8Template.class) {
99 if( !instances.containsKey(CryptoParametersJ8.DEFAULT_TYPE) )
100 {
101 try {
102 instances.put(CryptoParametersJ8.DEFAULT_TYPE,
103 (CryptoParametersJ8.DEFAULT_TYPE.equals(TYPES.PBE))? new CryptoStreamPBEImpl():
104 new CryptoStreamGCMImpl()
105 );
106 } catch (GeneralSecurityException e) {
107 e.printStackTrace();
108 throw new RuntimeException(e.getMessage());
109 }
110 }
111
112 return instances.get(CryptoParametersJ8.DEFAULT_TYPE);
113 }
114 }
115
116
117
118
119
120
121
122 public static CryptoStreamFactoryJ8 getInstance(TYPES type)
123 {
124 synchronized (CryptoStreamFactoryJ8Template.class) {
125 if( !instances.containsKey(type) )
126 {
127 try {
128 instances.put(type,
129 (type.equals(TYPES.PBE))? new CryptoStreamPBEImpl():
130 new CryptoStreamGCMImpl()
131 );
132 } catch (GeneralSecurityException e) {
133 e.printStackTrace();
134 throw new RuntimeException(e.getMessage());
135 }
136 }
137
138 return instances.get(type);
139 }
140 }
141
142
143
144
145
146
147
148
149
150 public static CryptoStreamFactoryJ8 getInstance(TYPES type, byte[] salt, int count)
151 {
152 synchronized (CryptoStreamFactoryJ8Template.class) {
153 if( !instances.containsKey(type) )
154 {
155 try {
156 instances.put(type,
157 (type.equals(TYPES.PBE))? new CryptoStreamPBEImpl(salt, count):
158 new CryptoStreamGCMImpl(salt)
159 );
160 } catch (Exception e) {
161 e.printStackTrace();
162 throw new RuntimeException(e.getMessage());
163 }
164 }
165
166 return instances.get(type);
167 }
168 }
169
170
171
172
173
174
175 public InputStream getSmartInputStream(InputStream is)
176 throws GeneralSecurityException, IOException
177 {
178 return this.getSmartInputStream(
179 is,
180 PasswordFactory.getInstance("SHA-256").create()
181 );
182 }
183
184
185
186
187 public InputStream getInputStream( InputStream is, char[] password )
188 throws GeneralSecurityException, IOException
189 {
190 byte[] decrypted = this.createCipher( is, Cipher.DECRYPT_MODE, password.clone() );
191 InputStream eis = new ByteArrayInputStream(decrypted);
192 return eis;
193 }
194
195
196
197
198 public OutputStream getOutputStream(InputStream is, OutputStream os, char[] password)
199 throws GeneralSecurityException, IOException {
200 byte[] encrypted = this.createCipher( is, Cipher.ENCRYPT_MODE, password.clone() );
201 InputStream eis = new ByteArrayInputStream(encrypted);
202 StreamUtil.copy(eis, os);
203 return os;
204 }
205
206
207
208
209 public static void resetInstances()
210 {
211 CryptoStreamFactoryJ8Template.instances.clear();
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 @Override
228 public InputStream getInputStream(InputStream is, String decryptionMode)
229 throws GeneralSecurityException, IOException {
230 throw new UnsupportedOperationException("not implemented");
231 }
232
233 @Override
234 public InputStream getInputStream(InputStream is, String decryptionMode, char[] password)
235 throws GeneralSecurityException, IOException {
236 throw new UnsupportedOperationException("not implemented");
237 }
238
239 @Override
240 public InputStream getInputStream(InputStream is) throws GeneralSecurityException, IOException {
241 throw new UnsupportedOperationException("not implemented");
242 }
243
244 @Override
245 public InputStream getSmartInputStream(InputStream is, char[] password)
246 throws GeneralSecurityException, IOException {
247 throw new UnsupportedOperationException("not implemented");
248 }
249
250 @Override
251 public OutputStream getOutputStream(OutputStream os) throws GeneralSecurityException, IOException {
252 throw new UnsupportedOperationException("not implemented");
253 }
254
255 @Override
256 public OutputStream getOutputStream(OutputStream os, char[] password) throws GeneralSecurityException, IOException {
257 throw new UnsupportedOperationException("not implemented");
258 }
259
260
261
262
263
264
265
266
267
268
269 protected abstract Key createKey( char[] password, byte[] salt )
270 throws GeneralSecurityException;
271
272
273
274
275
276
277
278
279
280
281
282
283 protected abstract byte[] createCipher(InputStream is, int mode, char[] password )
284 throws GeneralSecurityException, IOException;
285
286
287
288
289
290
291
292
293
294
295
296 protected static byte[] generateSalt() throws GeneralSecurityException {
297 SecureRandom random;
298 try {
299 random = SecureRandom.getInstanceStrong();
300 byte[] salt = new byte[SALT_SIZE ];
301 random.nextBytes(salt);
302 return salt;
303 } catch (NoSuchAlgorithmException e) {
304 throw new GeneralSecurityException(e);
305 }
306 }
307
308 public byte[] getSalt() {
309 return salt.clone();
310 }
311
312 protected void setSalt(byte[] salt) {
313 this.salt = salt.clone();
314 }
315
316 public int getCount() {
317 return count;
318 }
319
320 public void setCount(int count) {
321 this.count = count;
322 }
323
324 public String getProviderName() {
325 return providerName;
326 }
327
328 public void setProviderName(String providerName) {
329 this.providerName = providerName;
330 }
331
332 public String getAlgorithm() {
333 return algorithm;
334 }
335
336 public void setAlgorithm(String algorithm) {
337 this.algorithm = algorithm;
338 }
339
340 }