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

File BcScryptKeyDerivationFunctionFactory.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

6
8
2
1
78
40
5
0.62
4
2
2.5

Classes

Class Line # Actions
BcScryptKeyDerivationFunctionFactory 45 8 0% 5 5
0.687568.8%
 

Contributing tests

This file is covered by 6 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 javax.inject.Singleton;
23   
24    import org.bouncycastle.asn1.ASN1Encodable;
25    import org.bouncycastle.asn1.ASN1ObjectIdentifier;
26    import org.bouncycastle.asn1.pkcs.KeyDerivationFunc;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.crypto.password.KeyDerivationFunction;
29    import org.xwiki.crypto.password.internal.kdf.BcScryptKDF;
30    import org.xwiki.crypto.password.internal.kdf.ScryptKDFParams;
31    import org.xwiki.crypto.password.params.KeyDerivationFunctionParameters;
32    import org.xwiki.crypto.password.params.ScryptParameters;
33   
34    /**
35    * Implementation of the key derivation function for Scrypt using Bouncy Castle.
36    *
37    * Functions provided by this factory are conform with http://www.tarsnap.com/scrypt/scrypt.pdf
38    * and the IETF expired draft draft-josefsson-scrypt-kdf-01.
39    *
40    * @version $Id: 091a8b3c7e5126df5fd507a8fe2699d1a6d33298 $
41    * @since 5.4M1
42    */
43    @Component(hints = { "Scrypt", "1.3.6.1.4.1.11591.4.11" })
44    @Singleton
 
45    public class BcScryptKeyDerivationFunctionFactory extends AbstractBcKDFFactory
46    {
47    /** This OID, part of the GNU space, is not really reserved but suggested byt the IETF expired draft. */
48    private static final ASN1ObjectIdentifier ALG_ID = new ASN1ObjectIdentifier("1.3.6.1.4.1.11591.4.11");
49   
 
50  9 toggle @Override
51    public KeyDerivationFunction getInstance(KeyDerivationFunctionParameters params)
52    {
53  9 if (!(params instanceof ScryptParameters)) {
54  0 throw new IllegalArgumentException("Invalid parameter used for Scrypt function: "
55    + params.getClass().getName());
56    }
57   
58  9 return new BcScryptKDF((ScryptParameters) params);
59    }
60   
 
61  3 toggle @Override
62    public KeyDerivationFunction getInstance(ASN1Encodable parameters)
63    {
64  3 KeyDerivationFunc kdf = KeyDerivationFunc.getInstance(parameters);
65   
66  3 if (!kdf.getAlgorithm().equals(ALG_ID)) {
67  0 throw new IllegalArgumentException("Illegal algorithm identifier for Scrypt: "
68    + kdf.getAlgorithm().getId());
69    }
70   
71  3 ScryptKDFParams params = ScryptKDFParams.getInstance(kdf.getParameters());
72   
73  3 return getInstance(
74  3 new ScryptParameters((params.getKeyLength() != null) ? params.getKeyLength().intValue() : -1,
75    params.getCostParameter().intValue(), params.getParallelizationParameter().intValue(),
76    params.getBlockSize().intValue(), params.getSalt()));
77    }
78    }