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

File ScryptParameters.java

 

Coverage histogram

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

Code metrics

0
19
13
1
196
73
13
0.68
1.46
13
1

Classes

Class Line # Actions
ScryptParameters 30 19 0% 13 8
0.7575%
 

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.params;
21   
22    import java.security.SecureRandom;
23   
24    /**
25    * Key derivation function parameters for Scrypt functions.
26    *
27    * @version $Id: f332481ea566da301559c953d9bf38ef727517ad $
28    * @since 5.4M1
29    */
 
30    public class ScryptParameters extends KeyDerivationFunctionParameters
31    {
32    private static final int SALT_DEFAULT_SIZE = 16;
33   
34    private static final int COST_DEFAULT = 1024;
35   
36    private static final int PARALLELIZATION_DEFAULT = 1;
37   
38    private static final int BLOCK_DEFAULT_SIZE = 8;
39   
40    private static final SecureRandom PRND = new SecureRandom();
41   
42    private final byte[] salt;
43   
44    private final int costParameter;
45   
46    private final int blockSize;
47   
48    private final int parallelizationParameter;
49   
50    /**
51    * Initialize all parameters to default values, using a random salt, a cost parameter of {@value #COST_DEFAULT},
52    * a parallelization parameter of {@value #PARALLELIZATION_DEFAULT}
53    * and a block size of {@value #BLOCK_DEFAULT_SIZE}.
54    */
 
55  0 toggle public ScryptParameters()
56    {
57  0 this(-1, COST_DEFAULT, PARALLELIZATION_DEFAULT, BLOCK_DEFAULT_SIZE, getRandomSalt());
58    }
59   
60    /**
61    * Initialize key size and use a random salt, a cost parameter of {@value #COST_DEFAULT},
62    * a parallelization parameter of {@value #PARALLELIZATION_DEFAULT}
63    * and a block size of {@value #BLOCK_DEFAULT_SIZE}.
64    *
65    * @param keySize Size of key to be generated in bytes. A negative value means that the key length should be
66    * smartly deducted from the context of use.
67    */
 
68  1 toggle public ScryptParameters(int keySize)
69    {
70  1 this(keySize, COST_DEFAULT, PARALLELIZATION_DEFAULT, BLOCK_DEFAULT_SIZE, getRandomSalt());
71    }
72   
73    /**
74    * Initialize parameters to custom values, a cost parameter of {@value #COST_DEFAULT}, a parallelization parameter
75    * of {@value #PARALLELIZATION_DEFAULT} and a block size of {@value #BLOCK_DEFAULT_SIZE}.
76    *
77    * @param keySize Size of key to be generated in bytes. A negative value means that the key length should be
78    * smartly deducted from the context of use.
79    * @param salt a salt.
80    */
 
81  0 toggle public ScryptParameters(int keySize, byte[] salt)
82    {
83  0 this(keySize, COST_DEFAULT, PARALLELIZATION_DEFAULT, BLOCK_DEFAULT_SIZE, salt);
84    }
85   
86    /**
87    * Initialize parameters to custom values, use a random salt and a block size of {@value #BLOCK_DEFAULT_SIZE}.
88    *
89    * @param keySize Size of key to be generated in bytes. A negative value means that the key length should be
90    * smartly deducted from the context of use.
91    * @param costParameter CPU/Memory cost parameter, must be larger than 1, a power of 2.
92    * @param parallelizationParameter Parallelization parameter, a positive integer less than or equal
93    * to (2^37-32) / 1024
94    */
 
95  1 toggle public ScryptParameters(int keySize, int costParameter, int parallelizationParameter)
96    {
97  1 this(keySize, costParameter, parallelizationParameter, BLOCK_DEFAULT_SIZE, getRandomSalt());
98    }
99   
100    /**
101    * Initialize parameters to custom values and use a block size of {@value #BLOCK_DEFAULT_SIZE}.
102    *
103    * @param keySize Size of key to be generated in bytes. A negative value means that the key length should be
104    * smartly deducted from the context of use.
105    * @param costParameter CPU/Memory cost parameter, must be larger than 1, a power of 2.
106    * @param parallelizationParameter Parallelization parameter, a positive integer less than or equal
107    * to (2^37-32) / 1024
108    * @param salt a salt.
109    */
 
110  0 toggle public ScryptParameters(int keySize, int costParameter, int parallelizationParameter, byte[] salt)
111    {
112  0 this(keySize, costParameter, parallelizationParameter, BLOCK_DEFAULT_SIZE, salt);
113    }
114   
115    /**
116    * Initialize all parameters to custom values and use a random salt.
117    *
118    * @param keySize Size of key to be generated in bytes. A negative value means that the key length should be
119    * smartly deducted from the context of use.
120    * @param costParameter CPU/Memory cost parameter, must be larger than 1, a power of 2
121    * and less than 2^(16 * blockSize)
122    * @param parallelizationParameter Parallelization parameter, a positive integer less than or equal
123    * to (2^37-32) / (128 * blockSize)
124    * @param blockSize Block size parameter.
125    */
 
126  0 toggle public ScryptParameters(int keySize, int costParameter, int parallelizationParameter, int blockSize)
127    {
128  0 this(keySize, costParameter, parallelizationParameter, blockSize, getRandomSalt());
129    }
130   
131    /**
132    * Initialize all parameters to custom values.
133    *
134    * @param keySize Size of key to be generated in bytes. A negative value means that the key length should be
135    * smartly deducted from the context of use.
136    * @param costParameter CPU/Memory cost parameter, must be larger than 1, a power of 2
137    * and less than 2^(16 * blockSize)
138    * @param parallelizationParameter Parallelization parameter, a positive integer less than or equal
139    * to (2^37-32) / (128 * blockSize)
140    * @param blockSize Block size parameter.
141    * @param salt a salt.
142    */
 
143  9 toggle public ScryptParameters(int keySize, int costParameter, int parallelizationParameter, int blockSize, byte[] salt)
144    {
145  9 super(keySize);
146  9 this.costParameter = costParameter;
147  9 this.blockSize = blockSize;
148  9 this.parallelizationParameter = parallelizationParameter;
149  9 this.salt = salt;
150    }
151   
 
152  2 toggle private static byte[] getRandomSalt()
153    {
154  2 byte[] salt = new byte[SALT_DEFAULT_SIZE];
155  2 PRND.nextBytes(salt);
156  2 return salt;
157    }
158   
 
159  2 toggle @Override
160    public String getAlgorithmName()
161    {
162  2 return "Scrypt";
163    }
164   
165    /**
166    * @return the CPU/Memory cost parameter N.
167    */
 
168  14 toggle public int getCostParameter()
169    {
170  14 return this.costParameter;
171    }
172   
173    /**
174    * @return the parallelization parameter.
175    */
 
176  14 toggle public int getParallelizationParameter()
177    {
178  14 return this.parallelizationParameter;
179    }
180   
181    /**
182    * @return the salt value.
183    */
 
184  14 toggle public byte[] getSalt()
185    {
186  14 return this.salt;
187    }
188   
189    /**
190    * @return the block size parameter r.
191    */
 
192  14 toggle public int getBlockSize()
193    {
194  14 return this.blockSize;
195    }
196    }