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.io.OutputStream;
25 import java.security.GeneralSecurityException;
26
27 import javax.crypto.Cipher;
28 import javax.crypto.CipherInputStream;
29 import javax.crypto.CipherOutputStream;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public abstract class CryptoStreamFactoryTemplate implements CryptoStreamFactory
52 {
53
54
55 protected static CryptoStreamFactory instance;
56
57 public static CryptoStreamFactory getInstance()
58 {
59 return instance;
60 }
61
62 public static void setInstance(CryptoStreamFactory instance)
63 {
64 CryptoStreamFactoryTemplate.instance = instance;
65 }
66
67
68
69
70 public InputStream getInputStream(InputStream is, String decryptionMode) throws GeneralSecurityException, IOException
71 {
72
73 InputStream result = null;
74
75 if( "auto".equalsIgnoreCase(decryptionMode) )
76 {
77 result = getSmartInputStream(is);
78 }
79 else if( "true".equalsIgnoreCase(decryptionMode) )
80 {
81 result = getInputStream(is);
82 }
83 else
84 {
85 result = is;
86 }
87 return result;
88 }
89
90
91
92
93 public InputStream getInputStream(InputStream is, String decryptionMode, char[] password) throws GeneralSecurityException, IOException
94 {
95
96 InputStream result = null;
97
98 if( "auto".equalsIgnoreCase(decryptionMode) )
99 {
100 result = getSmartInputStream(is, password);
101 }
102 else if( "true".equalsIgnoreCase(decryptionMode) )
103 {
104 result = getInputStream(is, password);
105 }
106 else
107 {
108 result = is;
109 }
110 return result;
111 }
112
113
114
115
116 public InputStream getInputStream( InputStream is )
117 throws GeneralSecurityException, IOException
118 {
119 Cipher cipher = this.createCipher( Cipher.DECRYPT_MODE, PasswordFactory.getInstance().create() );
120 return new CipherInputStream( is, cipher );
121 }
122
123
124
125
126 public InputStream getInputStream( InputStream is, char[] password )
127 throws GeneralSecurityException, IOException
128 {
129 Cipher cipher = this.createCipher( Cipher.DECRYPT_MODE, password );
130 return new CipherInputStream( is, cipher );
131 }
132
133
134
135
136 public InputStream getSmartInputStream(InputStream is)
137 throws GeneralSecurityException, IOException
138 {
139 return this.getSmartInputStream(
140 is,
141 PasswordFactory.getInstance().create()
142 );
143 }
144
145
146
147
148 public abstract InputStream getSmartInputStream(InputStream is, char[] password )
149 throws GeneralSecurityException, IOException;
150
151
152
153
154 public OutputStream getOutputStream( OutputStream os )
155 throws GeneralSecurityException, IOException
156 {
157 Cipher cipher = this.createCipher( Cipher.ENCRYPT_MODE, PasswordFactory.getInstance().create() );
158 return new CipherOutputStream( os, cipher ); }
159
160
161
162
163 public OutputStream getOutputStream( OutputStream os, char[] password )
164 throws GeneralSecurityException, IOException
165 {
166 Cipher cipher = this.createCipher( Cipher.ENCRYPT_MODE, password );
167 return new CipherOutputStream( os, cipher );
168 }
169 protected abstract Cipher createCipher(int encryptMode, char[] password) throws GeneralSecurityException, IOException;
170
171 }