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