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

File PBKDF2Params.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

20
46
12
1
227
120
22
0.48
3.83
12
1.83

Classes

Class Line # Actions
PBKDF2Params 42 46 0% 22 15
0.807692380.8%
 

Contributing tests

This file is covered by 14 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.math.BigInteger;
23    import java.util.Enumeration;
24   
25    import org.bouncycastle.asn1.ASN1Encodable;
26    import org.bouncycastle.asn1.ASN1EncodableVector;
27    import org.bouncycastle.asn1.ASN1Integer;
28    import org.bouncycastle.asn1.ASN1Object;
29    import org.bouncycastle.asn1.ASN1OctetString;
30    import org.bouncycastle.asn1.ASN1Primitive;
31    import org.bouncycastle.asn1.ASN1Sequence;
32    import org.bouncycastle.asn1.DEROctetString;
33    import org.bouncycastle.asn1.DERSequence;
34    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
35   
36    /**
37    * Replace {@link org.bouncycastle.asn1.pkcs.PBKDF2Params} to support alternative Digest as defined in PKCS #5 v2.1.
38    *
39    * @version $Id: 1e563a8d74b6392751b63f740f32f815e60358a3 $
40    * @since 5.4M1
41    */
 
42    public class PBKDF2Params extends ASN1Object
43    {
44    private final ASN1OctetString octStr;
45   
46    private final ASN1Integer iterationCount;
47   
48    private final ASN1Integer keyLength;
49   
50    private final AlgorithmIdentifier prf;
51   
52    /**
53    * Initialize parameters without key length and an default SHA-1 pseudo random function.
54    *
55    * @param salt the salt.
56    * @param iterationCount the iteration count.
57    */
 
58  0 toggle public PBKDF2Params(byte[] salt, int iterationCount)
59    {
60  0 this(new DEROctetString(salt), new ASN1Integer(iterationCount), null, null);
61    }
62   
63    /**
64    * Initialize parameters without key length.
65    *
66    * @param salt the salt.
67    * @param iterationCount the iteration count.
68    * @param prf the pseudo random function identifier.
69    */
 
70  18 toggle public PBKDF2Params(byte[] salt, int iterationCount, AlgorithmIdentifier prf)
71    {
72  18 this(new DEROctetString(salt), new ASN1Integer(iterationCount), null, prf);
73    }
74   
75    /**
76    * Initialize parameters with a default SHA-1 pseudo random function.
77    *
78    * @param salt the salt.
79    * @param iterationCount the iteration count.
80    * @param keyLength the key length.
81    */
 
82  0 toggle public PBKDF2Params(byte[] salt, int iterationCount, int keyLength)
83    {
84  0 this(new DEROctetString(salt), new ASN1Integer(iterationCount), new ASN1Integer(keyLength), null);
85    }
86   
87    /**
88    * Initialize all parameters.
89    *
90    * @param salt the salt.
91    * @param iterationCount the iteration count.
92    * @param keyLength the key length.
93    * @param prf the pseudo random function identifier.
94    */
 
95  6 toggle public PBKDF2Params(byte[] salt, int iterationCount, int keyLength, AlgorithmIdentifier prf)
96    {
97  6 this(new DEROctetString(salt), new ASN1Integer(iterationCount), new ASN1Integer(keyLength), prf);
98    }
99   
 
100  24 toggle private PBKDF2Params(ASN1OctetString salt, ASN1Integer iterationCount, ASN1Integer keyLength,
101    AlgorithmIdentifier prf)
102    {
103  24 this.octStr = salt;
104  24 this.iterationCount = iterationCount;
105  24 this.keyLength = keyLength;
106  24 this.prf = prf;
107    }
108   
109    /**
110    * Build a new instance from ASN.1 sequence.
111    *
112    * @param seq an ASN.1 sequence corresponding to PBKDF2 parameters.
113    */
 
114  26 toggle private PBKDF2Params(ASN1Sequence seq)
115    {
116  26 @SuppressWarnings("unchecked")
117    Enumeration<ASN1Encodable> e = seq.getObjects();
118   
119  26 this.octStr = (ASN1OctetString) e.nextElement();
120  26 this.iterationCount = (ASN1Integer) e.nextElement();
121   
122  26 if (e.hasMoreElements()) {
123  14 Object obj = e.nextElement();
124  14 if (obj instanceof ASN1Integer) {
125  6 this.keyLength = (ASN1Integer) obj;
126  6 if (e.hasMoreElements()) {
127  0 this.prf = AlgorithmIdentifier.getInstance(obj);
128    } else {
129  6 this.prf = null;
130    }
131    } else {
132  8 this.keyLength = null;
133  8 this.prf = AlgorithmIdentifier.getInstance(obj);
134    }
135    } else {
136  12 this.keyLength = null;
137  12 this.prf = null;
138    }
139    }
140   
141    /**
142    * Static factory methods to create new instance from existing instance or encoded data.
143    *
144    * @param obj a compatible object, either another instance or a ASN.1 sequence.
145    * @return a new instance.
146    */
 
147  38 toggle public static PBKDF2Params getInstance(Object obj)
148    {
149  38 if (obj instanceof PBKDF2Params) {
150  12 return (PBKDF2Params) obj;
151    }
152   
153  26 if (obj instanceof org.bouncycastle.asn1.pkcs.PBKDF2Params) {
154  0 org.bouncycastle.asn1.pkcs.PBKDF2Params params = (org.bouncycastle.asn1.pkcs.PBKDF2Params) obj;
155  0 if (params.getKeyLength() != null) {
156  0 return new PBKDF2Params(params.getSalt(),
157    params.getIterationCount().intValue(), params.getKeyLength().intValue());
158    } else {
159  0 return new PBKDF2Params(params.getSalt(), params.getIterationCount().intValue());
160    }
161    }
162   
163  26 if (obj != null) {
164  26 return new PBKDF2Params(ASN1Sequence.getInstance(obj));
165    }
166   
167  0 return null;
168    }
169   
170    /**
171    * @return the salt.
172    */
 
173  14 toggle public byte[] getSalt()
174    {
175  14 return this.octStr.getOctets();
176    }
177   
178    /**
179    * @return the iteration count.
180    */
 
181  14 toggle public BigInteger getIterationCount()
182    {
183  14 return this.iterationCount.getValue();
184    }
185   
186    /**
187    * @return the key length.
188    */
 
189  18 toggle public BigInteger getKeyLength()
190    {
191  18 if (this.keyLength != null) {
192  8 return this.keyLength.getValue();
193    }
194   
195  10 return null;
196    }
197   
198    /**
199    * @return the pseudo random function identifier, defaulting to SHA-1.
200    */
 
201  14 toggle public AlgorithmIdentifier getPseudoRandomFunctionIdentifier()
202    {
203  14 return this.prf;
204    }
205   
206    /**
207    * @return the underlying primitive type.
208    */
 
209  99 toggle @Override
210    public ASN1Primitive toASN1Primitive()
211    {
212  99 ASN1EncodableVector v = new ASN1EncodableVector();
213   
214  99 v.add(this.octStr);
215  99 v.add(this.iterationCount);
216   
217  99 if (this.keyLength != null) {
218  21 v.add(this.keyLength);
219    }
220   
221  99 if (this.prf != null) {
222  48 v.add(this.prf);
223    }
224   
225  99 return new DERSequence(v);
226    }
227    }