1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.password.internal.kdf.factory

File DefaultKeyDerivationFunctionFactoryTest.java

 

Code metrics

0
20
4
1
94
61
4
0.2
5
4
1

Classes

Class Line # Actions
DefaultKeyDerivationFunctionFactoryTest 41 20 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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})
 
41    public class DefaultKeyDerivationFunctionFactoryTest
42    {
43    @Rule
44    public final MockitoComponentMockingRule<KeyDerivationFunctionFactory> mocker =
45    new MockitoComponentMockingRule<KeyDerivationFunctionFactory>(DefaultKeyDerivationFunctionFactory.class);
46   
47    KeyDerivationFunctionFactory factory;
48   
 
49  2 toggle @Before
50    public void configure() throws Exception
51    {
52  2 factory = mocker.getComponentUnderTest();
53    }
54   
 
55  2 toggle KeyDerivationFunction getKDFInstance(KeyDerivationFunctionParameters parameters)
56    {
57  2 return factory.getInstance(parameters);
58    }
59   
 
60  1 toggle @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   
 
78  1 toggle @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    }