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

File BcScryptKeyDerivationFunctionFactoryTest.java

 

Code metrics

0
13
7
1
112
75
7
0.54
1.86
7
1

Classes

Class Line # Actions
BcScryptKeyDerivationFunctionFactoryTest 41 13 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 5 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.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    * Please comment here
38    *
39    * @version $Id: 970b6a360436f1d134d4c16d2e7ae8b5cfbdd401 $
40    */
 
41    public class BcScryptKeyDerivationFunctionFactoryTest
42    {
43    @Rule
44    public final MockitoComponentMockingRule<KeyDerivationFunctionFactory> mocker =
45    new MockitoComponentMockingRule<KeyDerivationFunctionFactory>(BcScryptKeyDerivationFunctionFactory.class);
46   
47    KeyDerivationFunctionFactory factory;
48   
 
49  5 toggle @Before
50    public void configure() throws Exception
51    {
52  5 factory = mocker.getComponentUnderTest();
53    }
54   
 
55  4 toggle KeyDerivationFunction getKDFInstance(ScryptParameters parameters)
56    {
57  4 return factory.getInstance(parameters);
58    }
59   
 
60  1 toggle @Test
61    public void scryptPropertiesTest() throws Exception
62    {
63  1 assertThat(factory.getKDFAlgorithmName(), equalTo("Scrypt"));
64    }
65   
 
66  1 toggle @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   
 
77  1 toggle @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   
 
88  1 toggle @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   
 
99  1 toggle @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    }