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 | |
|
16 | |
import java.security.GeneralSecurityException; |
17 | |
import java.security.spec.AlgorithmParameterSpec; |
18 | |
import java.security.spec.KeySpec; |
19 | |
|
20 | |
import javax.crypto.SecretKey; |
21 | |
import javax.crypto.SecretKeyFactory; |
22 | |
import javax.crypto.spec.PBEKeySpec; |
23 | |
import javax.crypto.spec.PBEParameterSpec; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class PasswordBasedEncryptionStrategy extends AbstractJCEEncryptionStrategy |
31 | |
{ |
32 | |
|
33 | |
public static final String DEFAULT_ALGORITHM = "PBEWithMD5AndDES"; |
34 | |
public static final int DEFAULT_ITERATION_COUNT = 20; |
35 | |
|
36 | 12 | private byte[] salt = null; |
37 | |
|
38 | 12 | private int iterationCount = DEFAULT_ITERATION_COUNT; |
39 | |
|
40 | |
private char[] password; |
41 | |
|
42 | |
public PasswordBasedEncryptionStrategy() |
43 | 12 | { |
44 | 12 | algorithm = DEFAULT_ALGORITHM; |
45 | 12 | } |
46 | |
|
47 | |
public void initialise() throws InitialisationException |
48 | |
{ |
49 | 12 | if (salt == null) |
50 | |
{ |
51 | 12 | salt = new byte[]{(byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, |
52 | |
(byte) 0xee, (byte) 0x99}; |
53 | 12 | logger.debug("Salt is not set. Using default salt"); |
54 | |
} |
55 | |
|
56 | 12 | if (password == null) |
57 | |
{ |
58 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("Password"), this); |
59 | |
} |
60 | 12 | super.initialise(); |
61 | 12 | } |
62 | |
|
63 | |
protected KeySpec createKeySpec() |
64 | |
{ |
65 | 12 | return new PBEKeySpec(password); |
66 | |
} |
67 | |
|
68 | |
protected AlgorithmParameterSpec createAlgorithmParameterSpec() |
69 | |
{ |
70 | 12 | return new PBEParameterSpec(salt, iterationCount); |
71 | |
} |
72 | |
|
73 | |
public byte[] getSalt() |
74 | |
{ |
75 | 0 | return salt; |
76 | |
} |
77 | |
|
78 | |
public void setSalt(byte[] salt) |
79 | |
{ |
80 | 0 | this.salt = salt; |
81 | 0 | } |
82 | |
|
83 | |
public int getIterationCount() |
84 | |
{ |
85 | 0 | return iterationCount; |
86 | |
} |
87 | |
|
88 | |
public void setIterationCount(int iterationCount) |
89 | |
{ |
90 | 0 | this.iterationCount = iterationCount; |
91 | 0 | } |
92 | |
|
93 | |
public void setPassword(String password) |
94 | |
{ |
95 | 12 | this.password = password.toCharArray(); |
96 | 12 | } |
97 | |
|
98 | |
protected SecretKey getSecretKey() throws GeneralSecurityException |
99 | |
{ |
100 | 12 | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(getAlgorithm()); |
101 | 12 | return keyFactory.generateSecret(keySpec); |
102 | |
} |
103 | |
} |