1 | |
package org.apache.fulcrum.pbe; |
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 org.apache.avalon.framework.configuration.Configurable; |
28 | |
import org.apache.avalon.framework.configuration.Configuration; |
29 | |
import org.apache.avalon.framework.configuration.ConfigurationException; |
30 | |
import org.apache.avalon.framework.logger.AbstractLogEnabled; |
31 | |
import org.apache.fulcrum.jce.crypto.CryptoParameters; |
32 | |
import org.apache.fulcrum.jce.crypto.CryptoStreamFactory; |
33 | |
import org.apache.fulcrum.jce.crypto.CryptoStreamFactoryImpl; |
34 | |
import org.apache.fulcrum.jce.crypto.CryptoUtil; |
35 | |
import org.apache.fulcrum.jce.crypto.HexConverter; |
36 | |
import org.apache.fulcrum.jce.crypto.PasswordFactory; |
37 | |
import org.apache.fulcrum.jce.crypto.PasswordParameters; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class PBEServiceImpl |
47 | |
extends AbstractLogEnabled |
48 | |
implements PBEService, Configurable |
49 | |
{ |
50 | |
|
51 | |
private CryptoStreamFactory cryptoStreamFactory; |
52 | |
|
53 | |
|
54 | |
private byte[] passwordSalt; |
55 | |
|
56 | |
|
57 | |
private int passwordCount; |
58 | |
|
59 | |
|
60 | |
private char[] defaultPassword; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public PBEServiceImpl() |
66 | 0 | { |
67 | |
|
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public void configure(Configuration configuration) |
78 | |
throws ConfigurationException |
79 | |
{ |
80 | |
|
81 | |
|
82 | 0 | byte[] cryptoSalt = CryptoParameters.SALT; |
83 | 0 | int cryptoCount = configuration.getChild("cyrptoCount").getValueAsInteger(CryptoParameters.COUNT); |
84 | 0 | String tempCryptoSalt = configuration.getChild("cryptoSalt").getValue(""); |
85 | |
|
86 | 0 | if( tempCryptoSalt.length() > 0 ) |
87 | |
{ |
88 | 0 | cryptoSalt = HexConverter.toBytes( tempCryptoSalt ); |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | 0 | this.cryptoStreamFactory = new CryptoStreamFactoryImpl( |
94 | |
cryptoSalt, |
95 | |
cryptoCount |
96 | |
); |
97 | |
|
98 | |
|
99 | |
|
100 | 0 | this.passwordSalt = PasswordParameters.SALT; |
101 | 0 | this.passwordCount = configuration.getChild("passwordCount").getValueAsInteger(PasswordParameters.COUNT); |
102 | 0 | this.defaultPassword = PasswordParameters.DEFAULTPASSWORD; |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public char[] createPassword() throws Exception |
114 | |
{ |
115 | 0 | return PasswordFactory.create( |
116 | |
this.defaultPassword, |
117 | |
this.passwordSalt, |
118 | |
this.passwordCount |
119 | |
); |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public char [] createPassword(char [] seed) throws Exception |
126 | |
{ |
127 | 0 | return PasswordFactory.create( |
128 | |
seed, |
129 | |
this.passwordSalt, |
130 | |
this.passwordCount |
131 | |
); |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public String decryptString(String cipherText, char [] password) |
138 | |
throws GeneralSecurityException, IOException |
139 | |
{ |
140 | 0 | return CryptoUtil.decryptString( |
141 | |
this.getCryptoStreamFactory(), |
142 | |
cipherText, |
143 | |
password |
144 | |
); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public String encryptString(String plainText, char [] password) |
151 | |
throws GeneralSecurityException, IOException |
152 | |
{ |
153 | 0 | return CryptoUtil.encryptString( |
154 | |
this.getCryptoStreamFactory(), |
155 | |
plainText, |
156 | |
password |
157 | |
); |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public InputStream getInputStream(InputStream is, char [] password) |
164 | |
throws GeneralSecurityException, IOException |
165 | |
{ |
166 | 0 | return this.getCryptoStreamFactory().getInputStream( |
167 | |
is, |
168 | |
password |
169 | |
); |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public InputStream getSmartInputStream(InputStream is, char [] password) |
176 | |
throws GeneralSecurityException, IOException |
177 | |
{ |
178 | 0 | return this.getCryptoStreamFactory().getSmartInputStream( |
179 | |
is, |
180 | |
password |
181 | |
); |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public OutputStream getOutputStream(OutputStream os, char [] password) |
188 | |
throws GeneralSecurityException, IOException |
189 | |
{ |
190 | 0 | return this.getCryptoStreamFactory().getOutputStream( |
191 | |
os, |
192 | |
password |
193 | |
); |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public void decrypt(Object source, Object target, char [] password) |
200 | |
throws GeneralSecurityException, IOException |
201 | |
{ |
202 | 0 | CryptoUtil.decrypt( |
203 | |
this.getCryptoStreamFactory(), |
204 | |
source, |
205 | |
target, |
206 | |
password |
207 | |
); |
208 | 0 | } |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public void encrypt(Object source, Object target, char [] password) |
214 | |
throws GeneralSecurityException, IOException |
215 | |
{ |
216 | 0 | CryptoUtil.encrypt( |
217 | |
this.getCryptoStreamFactory(), |
218 | |
source, |
219 | |
target, |
220 | |
password |
221 | |
); |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
private CryptoStreamFactory getCryptoStreamFactory() |
232 | |
{ |
233 | 0 | return cryptoStreamFactory; |
234 | |
} |
235 | |
} |