org.bouncycastle.crypto.util.ScryptConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk15to18 Show documentation
Show all versions of bcprov-jdk15to18 Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.
package org.bouncycastle.crypto.util;
import org.bouncycastle.internal.asn1.misc.MiscObjectIdentifiers;
/**
* Configuration class for a PBKDF based around scrypt.
*/
public class ScryptConfig
extends PBKDFConfig
{
public static class Builder
{
private final int costParameter;
private final int blockSize;
private final int parallelizationParameter;
private int saltLength = 16;
/**
* Base constructor.
*
* @param costParameter cost parameter (must be a power of 2)
* @param blockSize block size
* @param parallelizationParameter parallelization parameter
*/
public Builder(int costParameter, int blockSize, int parallelizationParameter)
{
if (costParameter <= 1 || !isPowerOf2(costParameter))
{
throw new IllegalArgumentException("Cost parameter N must be > 1 and a power of 2");
}
this.costParameter = costParameter;
this.blockSize = blockSize;
this.parallelizationParameter = parallelizationParameter;
}
/**
* Set the length of the salt to use.
*
* @param saltLength the length of the salt (in octets) to use.
* @return the current builder.
*/
public Builder withSaltLength(int saltLength)
{
this.saltLength = saltLength;
return this;
}
public ScryptConfig build()
{
return new ScryptConfig(this);
}
// note: we know X is non-zero
private static boolean isPowerOf2(int x)
{
return ((x & (x - 1)) == 0);
}
}
private final int costParameter;
private final int blockSize;
private final int parallelizationParameter;
private final int saltLength;
private ScryptConfig(Builder builder)
{
super(MiscObjectIdentifiers.id_scrypt);
this.costParameter = builder.costParameter;
this.blockSize = builder.blockSize;
this.parallelizationParameter = builder.parallelizationParameter;
this.saltLength = builder.saltLength;
}
public int getCostParameter()
{
return costParameter;
}
public int getBlockSize()
{
return blockSize;
}
public int getParallelizationParameter()
{
return parallelizationParameter;
}
public int getSaltLength()
{
return saltLength;
}
}