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

File PBES2Parameters.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

8
21
6
1
130
65
10
0.48
3.5
6
1.67

Classes

Class Line # Actions
PBES2Parameters 40 21 0% 10 6
0.8285714482.9%
 

Contributing tests

This file is covered by 13 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.util.Enumeration;
23   
24    import org.bouncycastle.asn1.ASN1Encodable;
25    import org.bouncycastle.asn1.ASN1EncodableVector;
26    import org.bouncycastle.asn1.ASN1Object;
27    import org.bouncycastle.asn1.ASN1Primitive;
28    import org.bouncycastle.asn1.ASN1Sequence;
29    import org.bouncycastle.asn1.DERSequence;
30    import org.bouncycastle.asn1.pkcs.EncryptionScheme;
31    import org.bouncycastle.asn1.pkcs.KeyDerivationFunc;
32    import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
33   
34    /**
35    * Replace {@link org.bouncycastle.asn1.pkcs.PBES2Parameters} to support alternative Digest as defined in PKCS #5 v2.1.
36    *
37    * @version $Id: e9c4f85507642442e038b99a49bb8373b65e2bba $
38    * @since 5.4M1
39    */
 
40    public class PBES2Parameters
41    extends ASN1Object
42    implements PKCSObjectIdentifiers
43    {
44    private KeyDerivationFunc func;
45   
46    private EncryptionScheme scheme;
47   
48    /**
49    * Initialize parameters.
50    *
51    * @param keyDevFunc the key derivation function definition.
52    * @param encScheme the encryption scheme definition.
53    */
 
54  25 toggle public PBES2Parameters(KeyDerivationFunc keyDevFunc, EncryptionScheme encScheme)
55    {
56  25 this.func = keyDevFunc;
57  25 this.scheme = encScheme;
58    }
59   
60    /**
61    * Build a new instance from ASN.1 sequence.
62    *
63    * @param seq an ASN.1 sequence corresponding to PBES2 parameters.
64    */
 
65  26 toggle private PBES2Parameters(ASN1Sequence seq)
66    {
67  26 @SuppressWarnings("unchecked")
68    Enumeration<ASN1Encodable> e = seq.getObjects();
69  26 ASN1Sequence funcSeq = ASN1Sequence.getInstance(e.nextElement().toASN1Primitive());
70   
71  26 if (funcSeq.getObjectAt(0).equals(id_PBKDF2)) {
72  24 this.func = new KeyDerivationFunc(id_PBKDF2, PBKDF2Params.getInstance(funcSeq.getObjectAt(1)));
73    } else {
74  2 this.func = KeyDerivationFunc.getInstance(funcSeq);
75    }
76   
77  26 this.scheme = EncryptionScheme.getInstance(e.nextElement());
78    }
79   
80    /**
81    * Static factory methods to create new instance from existing instance or encoded data.
82    *
83    * @param obj a compatible object, either another instance or a ASN.1 sequence.
84    * @return a new instance.
85    */
 
86  26 toggle public static PBES2Parameters getInstance(Object obj)
87    {
88  26 if (obj instanceof PBES2Parameters) {
89  0 return (PBES2Parameters) obj;
90    }
91  26 if (obj instanceof org.bouncycastle.asn1.pkcs.PBES2Parameters) {
92  0 return new PBES2Parameters(((org.bouncycastle.asn1.pkcs.PBES2Parameters) obj).getKeyDerivationFunc(),
93    ((org.bouncycastle.asn1.pkcs.PBES2Parameters) obj).getEncryptionScheme());
94    }
95  26 if (obj != null) {
96  26 return new PBES2Parameters(ASN1Sequence.getInstance(obj));
97    }
98  0 return null;
99    }
100   
101    /**
102    * @return the key derivation function definition.
103    */
 
104  13 toggle public KeyDerivationFunc getKeyDerivationFunc()
105    {
106  13 return this.func;
107    }
108   
109    /**
110    * @return the encryption scheme definition.
111    */
 
112  26 toggle public EncryptionScheme getEncryptionScheme()
113    {
114  26 return this.scheme;
115    }
116   
117    /**
118    * @return the underlying primitive type.
119    */
 
120  57 toggle @Override
121    public ASN1Primitive toASN1Primitive()
122    {
123  57 ASN1EncodableVector v = new ASN1EncodableVector();
124   
125  57 v.add(this.func);
126  57 v.add(this.scheme);
127   
128  57 return new DERSequence(v);
129    }
130    }