1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.password.internal.kdf.factory; |
21 |
|
|
22 |
|
import org.bouncycastle.util.encoders.Hex; |
23 |
|
import org.junit.Before; |
24 |
|
import org.junit.Rule; |
25 |
|
import org.junit.Test; |
26 |
|
import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters; |
27 |
|
import org.xwiki.crypto.password.KeyDerivationFunction; |
28 |
|
import org.xwiki.crypto.password.KeyDerivationFunctionFactory; |
29 |
|
import org.xwiki.crypto.password.PasswordToByteConverter; |
30 |
|
import org.xwiki.crypto.password.params.ScryptParameters; |
31 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
32 |
|
|
33 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
34 |
|
import static org.junit.Assert.assertThat; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
@version |
40 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 7 |
Complexity Density: 0.54 |
|
41 |
|
public class BcScryptKeyDerivationFunctionFactoryTest |
42 |
|
{ |
43 |
|
@Rule |
44 |
|
public final MockitoComponentMockingRule<KeyDerivationFunctionFactory> mocker = |
45 |
|
new MockitoComponentMockingRule<KeyDerivationFunctionFactory>(BcScryptKeyDerivationFunctionFactory.class); |
46 |
|
|
47 |
|
KeyDerivationFunctionFactory factory; |
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
49 |
5 |
@Before... |
50 |
|
public void configure() throws Exception |
51 |
|
{ |
52 |
5 |
factory = mocker.getComponentUnderTest(); |
53 |
|
} |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
55 |
4 |
KeyDerivationFunction getKDFInstance(ScryptParameters parameters)... |
56 |
|
{ |
57 |
4 |
return factory.getInstance(parameters); |
58 |
|
} |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
60 |
1 |
@Test... |
61 |
|
public void scryptPropertiesTest() throws Exception |
62 |
|
{ |
63 |
1 |
assertThat(factory.getKDFAlgorithmName(), equalTo("Scrypt")); |
64 |
|
} |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
66 |
1 |
@Test... |
67 |
|
public void scryptConformanceTest1() throws Exception |
68 |
|
{ |
69 |
1 |
assertThat(getKDFInstance(new ScryptParameters(64, 16, 1, 1, new byte[0])) |
70 |
|
.derive(new byte[0]).getKey(), |
71 |
|
equalTo(Hex.decode("77 d6 57 62 38 65 7b 20 3b 19 ca 42 c1 8a 04 97" |
72 |
|
+ "f1 6b 48 44 e3 07 4a e8 df df fa 3f ed e2 14 42" |
73 |
|
+ "fc d0 06 9d ed 09 48 f8 32 6a 75 3a 0f c8 1f 17" |
74 |
|
+ "e8 d3 e0 fb 2e 0d 36 28 cf 35 e2 0c 38 d1 89 06"))); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
77 |
1 |
@Test... |
78 |
|
public void scryptConformanceTest2() throws Exception |
79 |
|
{ |
80 |
1 |
assertThat(getKDFInstance(new ScryptParameters(64, 1024, 16, 8, new byte[] {'N', 'a', 'C', 'l'})) |
81 |
|
.derive(new byte[] {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}).getKey(), |
82 |
|
equalTo(Hex.decode("fd ba be 1c 9d 34 72 00 78 56 e7 19 0d 01 e9 fe" |
83 |
|
+ "7c 6a d7 cb c8 23 78 30 e7 73 76 63 4b 37 31 62" |
84 |
|
+ "2e af 30 d9 2e 22 a3 88 6f f1 09 27 9d 98 30 da" |
85 |
|
+ "c7 27 af b9 4a 83 ee 6d 83 60 cb df a2 cc 06 40"))); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
88 |
1 |
@Test... |
89 |
|
public void scryptConformanceTest3() throws Exception |
90 |
|
{ |
91 |
1 |
assertThat(getKDFInstance(new ScryptParameters(64, 16384, 1, 8, new byte[] {'S', 'o', 'd', 'i', 'u', 'm', 'C', 'h', 'l', 'o', 'r', 'i', 'd', 'e'})) |
92 |
|
.derive(new byte[] {'p', 'l', 'e', 'a', 's', 'e', 'l', 'e', 't', 'm', 'e', 'i', 'n'}).getKey(), |
93 |
|
equalTo(Hex.decode("70 23 bd cb 3a fd 73 48 46 1c 06 cd 81 fd 38 eb" |
94 |
|
+ "fd a8 fb ba 90 4f 8e 3e a9 b5 43 f6 54 5d a1 f2" |
95 |
|
+ "d5 43 29 55 61 3f 0f cf 62 d4 97 05 24 2a 9a f9" |
96 |
|
+ "e6 1e 85 dc 0d 65 1e 40 df cf 01 7b 45 57 58 87"))); |
97 |
|
} |
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
99 |
1 |
@Test... |
100 |
|
public void scryptSerializationDeserializationTest() throws Exception |
101 |
|
{ |
102 |
1 |
byte[] password = PasswordToByteConverter.convert("password"); |
103 |
1 |
KeyDerivationFunction kdf = getKDFInstance(new ScryptParameters(64, 512, 8)); |
104 |
1 |
KeyWithIVParameters params = kdf.derive(password, 8); |
105 |
|
|
106 |
1 |
KeyDerivationFunction kdf2 = factory.getInstance(kdf.getEncoded()); |
107 |
1 |
KeyWithIVParameters params2 = kdf2.derive(password, 8); |
108 |
|
|
109 |
1 |
assertThat(params.getKey(), equalTo(params2.getKey())); |
110 |
1 |
assertThat(params2.getIV(), equalTo(params2.getIV())); |
111 |
|
} |
112 |
|
} |