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

File AbstractBcPBKDF2.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
14
6
1
99
53
7
0.5
2.33
6
1.17

Classes

Class Line # Actions
AbstractBcPBKDF2 38 14 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 25 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;
21   
22    import java.io.IOException;
23   
24    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
25    import org.bouncycastle.crypto.PBEParametersGenerator;
26    import org.bouncycastle.crypto.params.KeyParameter;
27    import org.bouncycastle.crypto.params.ParametersWithIV;
28    import org.xwiki.crypto.params.cipher.symmetric.KeyWithIVParameters;
29    import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters;
30    import org.xwiki.crypto.password.params.PBKDF2Parameters;
31   
32    /**
33    * Abstract base class implement key derivation function that encapsulate Bouncy Castle PBE generator.
34    *
35    * @version $Id: 3d70419b2db9be7c81e8b29b28b16d1d5a65f78e $
36    * @since 5.4M1
37    */
 
38    public abstract class AbstractBcPBKDF2 extends AbstractBcKDF
39    {
40    private final PBEParametersGenerator generator;
41   
42    private final PBKDF2Parameters parameters;
43   
44    private final AlgorithmIdentifier algId;
45   
46    /**
47    * Construct a new derivation function based on the given generator.
48    *
49    * @param generator the Bouncy Castle generator to use.
50    * @param parameters the parameter for initializing the generator.
51    * @param algId the algorithm identifier of the pseudo random function used for this key derivation function.
52    */
 
53  45 toggle public AbstractBcPBKDF2(PBEParametersGenerator generator, PBKDF2Parameters parameters, AlgorithmIdentifier algId)
54    {
55  45 this.generator = generator;
56  45 this.parameters = parameters;
57  45 this.algId = algId;
58    }
59   
 
60  82 toggle @Override
61    public KeyDerivationFunctionParameters getParameters()
62    {
63  82 return this.parameters;
64    }
65   
66    /**
67    * @return the algorithm identifier of the pseudo random function used for this key derivation function.
68    */
 
69  24 toggle public AlgorithmIdentifier getPRFAlgorithmIdentifier()
70    {
71  24 if (this.parameters.getPseudoRandomFuntionHint() != null) {
72  12 return this.algId;
73    }
74  12 return null;
75    }
76   
 
77  34 toggle @Override
78    public org.xwiki.crypto.params.cipher.symmetric.KeyParameter derive(byte[] password)
79    {
80  34 this.generator.init(password, this.parameters.getSalt(), this.parameters.getIterationCount());
81  34 KeyParameter keyParam = (KeyParameter) this.generator.generateDerivedParameters(getKeySize() * 8);
82  34 return new org.xwiki.crypto.params.cipher.symmetric.KeyParameter(keyParam.getKey());
83    }
84   
 
85  11 toggle @Override
86    public KeyWithIVParameters derive(byte[] password, int ivSize)
87    {
88  11 this.generator.init(password, this.parameters.getSalt(), this.parameters.getIterationCount());
89  11 ParametersWithIV keyParam =
90    (ParametersWithIV) this.generator.generateDerivedParameters(getKeySize() * 8, ivSize * 8);
91  11 return new KeyWithIVParameters(((KeyParameter) keyParam.getParameters()).getKey(), keyParam.getIV());
92    }
93   
 
94  2 toggle @Override
95    public byte[] getEncoded() throws IOException
96    {
97  2 return getKeyDerivationFunction().getEncoded();
98    }
99    }