| 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.Base64; |
| 23 |
|
import org.bouncycastle.util.encoders.Hex; |
| 24 |
|
import org.junit.Before; |
| 25 |
|
import org.junit.Rule; |
| 26 |
|
import org.junit.Test; |
| 27 |
|
import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters; |
| 28 |
|
import org.xwiki.crypto.password.KeyDerivationFunction; |
| 29 |
|
import org.xwiki.crypto.password.KeyDerivationFunctionFactory; |
| 30 |
|
import org.xwiki.crypto.password.PasswordToByteConverter; |
| 31 |
|
import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters; |
| 32 |
|
import org.xwiki.crypto.password.params.PBKDF2Parameters; |
| 33 |
|
import org.xwiki.crypto.password.params.ScryptParameters; |
| 34 |
|
import org.xwiki.test.annotation.ComponentList; |
| 35 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
| 36 |
|
|
| 37 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
| 38 |
|
import static org.junit.Assert.assertThat; |
| 39 |
|
|
| 40 |
|
@ComponentList({BcScryptKeyDerivationFunctionFactory.class, BcPKCS5S2KeyDerivationFunctionFactory.class}) |
| |
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 4 |
Complexity Density: 0.2 |
|
| 41 |
|
public class DefaultKeyDerivationFunctionFactoryTest |
| 42 |
|
{ |
| 43 |
|
@Rule |
| 44 |
|
public final MockitoComponentMockingRule<KeyDerivationFunctionFactory> mocker = |
| 45 |
|
new MockitoComponentMockingRule<KeyDerivationFunctionFactory>(DefaultKeyDerivationFunctionFactory.class); |
| 46 |
|
|
| 47 |
|
KeyDerivationFunctionFactory factory; |
| 48 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 49 |
2 |
@Before... |
| 50 |
|
public void configure() throws Exception |
| 51 |
|
{ |
| 52 |
2 |
factory = mocker.getComponentUnderTest(); |
| 53 |
|
} |
| 54 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 55 |
2 |
KeyDerivationFunction getKDFInstance(KeyDerivationFunctionParameters parameters)... |
| 56 |
|
{ |
| 57 |
2 |
return factory.getInstance(parameters); |
| 58 |
|
} |
| 59 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 60 |
1 |
@Test... |
| 61 |
|
public void Pbkdf2DecodingTest() throws Exception |
| 62 |
|
{ |
| 63 |
1 |
byte[] password = PasswordToByteConverter.convert("password"); |
| 64 |
1 |
byte[] encoded = Base64.decode("MCYGCSqGSIb3DQEFDDAZBBAcO15L0rQ01O2RJ5UhyqCdAgID6AIBIA=="); |
| 65 |
|
|
| 66 |
1 |
KeyDerivationFunction kdf = getKDFInstance(new PBKDF2Parameters(32, 1000, Hex.decode( |
| 67 |
|
"1c3b5e4bd2b434d4ed91279521caa09d"))); |
| 68 |
1 |
KeyWithIVParameters params = kdf.derive(password, 8); |
| 69 |
|
|
| 70 |
1 |
KeyDerivationFunction kdf2 = factory.getInstance(encoded); |
| 71 |
1 |
KeyWithIVParameters params2 = kdf2.derive(password, 8); |
| 72 |
|
|
| 73 |
1 |
assertThat(kdf.getEncoded(), equalTo(encoded)); |
| 74 |
1 |
assertThat(params.getKey(), equalTo(params2.getKey())); |
| 75 |
1 |
assertThat(params2.getIV(), equalTo(params2.getIV())); |
| 76 |
|
} |
| 77 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 78 |
1 |
@Test... |
| 79 |
|
public void ScryptDecodingTest() throws Exception |
| 80 |
|
{ |
| 81 |
1 |
byte[] password = PasswordToByteConverter.convert("password"); |
| 82 |
1 |
byte[] encoded = Base64.decode("MCwGCSsGAQQB2kcECzAfBBAcO15L0rQ01O2RJ5UhyqCdAgICAAIBEAIBAgIBIA=="); |
| 83 |
|
|
| 84 |
1 |
KeyDerivationFunction kdf = getKDFInstance(new ScryptParameters(32, 512, 2, 16, Hex.decode("1c3b5e4bd2b434d4ed91279521caa09d"))); |
| 85 |
1 |
KeyWithIVParameters params = kdf.derive(password, 8); |
| 86 |
|
|
| 87 |
1 |
KeyDerivationFunction kdf2 = factory.getInstance(encoded); |
| 88 |
1 |
KeyWithIVParameters params2 = kdf2.derive(password, 8); |
| 89 |
|
|
| 90 |
1 |
assertThat(kdf.getEncoded(), equalTo(encoded)); |
| 91 |
1 |
assertThat(params.getKey(), equalTo(params2.getKey())); |
| 92 |
1 |
assertThat(params2.getIV(), equalTo(params2.getIV())); |
| 93 |
|
} |
| 94 |
|
} |