1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.security; |
8 | |
|
9 | |
import org.mule.api.lifecycle.InitialisationException; |
10 | |
import org.mule.config.i18n.CoreMessages; |
11 | |
import org.mule.util.StringMessageUtils; |
12 | |
|
13 | |
import java.security.GeneralSecurityException; |
14 | |
import java.security.spec.AlgorithmParameterSpec; |
15 | |
import java.security.spec.KeySpec; |
16 | |
|
17 | |
import javax.crypto.KeyGenerator; |
18 | |
import javax.crypto.SecretKey; |
19 | |
import javax.crypto.spec.SecretKeySpec; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class SecretKeyEncryptionStrategy extends AbstractJCEEncryptionStrategy |
31 | |
{ |
32 | |
|
33 | |
public static final String DEFAULT_ALGORITHM = "Blowfish"; |
34 | |
|
35 | |
private byte[] key; |
36 | |
private SecretKeyFactory keyFactory; |
37 | |
|
38 | |
public SecretKeyEncryptionStrategy() |
39 | 0 | { |
40 | 0 | algorithm = DEFAULT_ALGORITHM; |
41 | 0 | } |
42 | |
|
43 | |
public void initialise() throws InitialisationException |
44 | |
{ |
45 | 0 | if (key == null) |
46 | |
{ |
47 | 0 | if (keyFactory == null) |
48 | |
{ |
49 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("Key / KeyFactory"), this); |
50 | |
} |
51 | |
else |
52 | |
{ |
53 | |
try |
54 | |
{ |
55 | 0 | key = keyFactory.getKey(); |
56 | |
} |
57 | 0 | catch (Exception e) |
58 | |
{ |
59 | 0 | throw new InitialisationException(e, this); |
60 | 0 | } |
61 | |
} |
62 | |
} |
63 | 0 | super.initialise(); |
64 | 0 | } |
65 | |
|
66 | |
protected KeySpec createKeySpec() |
67 | |
{ |
68 | 0 | return new SecretKeySpec(key, algorithm); |
69 | |
} |
70 | |
|
71 | |
protected AlgorithmParameterSpec createAlgorithmParameterSpec() |
72 | |
{ |
73 | 0 | return null; |
74 | |
} |
75 | |
|
76 | |
public void setKey(byte[] rawKey) |
77 | |
{ |
78 | 0 | this.key = rawKey; |
79 | 0 | } |
80 | |
|
81 | |
public void setKey(String rawKey) |
82 | |
{ |
83 | 0 | this.key = StringMessageUtils.getBytes(rawKey); |
84 | 0 | } |
85 | |
|
86 | |
public SecretKeyFactory getKeyFactory() |
87 | |
{ |
88 | 0 | return keyFactory; |
89 | |
} |
90 | |
|
91 | |
public void setKeyFactory(SecretKeyFactory keyFactory) |
92 | |
{ |
93 | 0 | this.keyFactory = keyFactory; |
94 | 0 | } |
95 | |
|
96 | |
protected SecretKey getSecretKey() throws GeneralSecurityException |
97 | |
{ |
98 | 0 | return KeyGenerator.getInstance(algorithm).generateKey(); |
99 | |
} |
100 | |
|
101 | |
} |