com.intel.crypto
Class PasswordKeyDerivationAlg
- java.lang.Object
-
- com.intel.crypto.PasswordKeyDerivationAlg
-
public abstract class PasswordKeyDerivationAlg extends java.lang.Object
This abstract class represents a key derivation algorithm, based on PBKDF2.
-
-
Field Summary
Fields Modifier and Type Field and Description static short
HASH_TYPE_SHA1
A constant indicating the usage of SHA1 hash in the key derivation algorithmstatic short
HASH_TYPE_SHA256
A constant indicating the usage of SHA256 hash in the key derivation algorithm.static short
PASSWORD_MAX_LENGTH
A constant indicating the maximum allowed password lengthstatic short
RANDOM_SALT_LENGTH
A constant indicating the size of the random salt that will be used if no salt is provided
-
Method Summary
Methods Modifier and Type Method and Description static PasswordKeyDerivationAlg
create()
Returns an instance of the PasswordKeyDerivationAlg classabstract void
deriveKey(byte[] password, short passwordIndex, short passwordLength, byte[] key, short keyIndex, short keyLength)
Creates a key based on the password provided.abstract short
getHashAlgorithm()
Returns the currently used hash algorithm.abstract int
getIterationCount()
Returns the current number of iterations the algorithm runsstatic int
getIterationMaxCount()
Returns maximum iteration countabstract short
getSalt(byte[] salt, short saltIndex)
Returns the currently used salt buffer.abstract short
getSaltSize()
Returns the currently salt sizeabstract void
setHashAlgorithm(short hashAlg)
Sets the hash algorithm to be used for key derivation.abstract void
setIterationCount(int count)
Sets the number of iterations the algorithm needs to run.abstract void
setSalt(byte[] salt, short saltIndex, short saltLength)
Sets the salt value for the algorithm.
-
-
-
Field Detail
-
HASH_TYPE_SHA1
public static final short HASH_TYPE_SHA1
A constant indicating the usage of SHA1 hash in the key derivation algorithm- See Also:
- Constant Field Values
-
HASH_TYPE_SHA256
public static final short HASH_TYPE_SHA256
A constant indicating the usage of SHA256 hash in the key derivation algorithm. This is the default algorithm- See Also:
- Constant Field Values
-
PASSWORD_MAX_LENGTH
public static final short PASSWORD_MAX_LENGTH
A constant indicating the maximum allowed password length- See Also:
- Constant Field Values
-
RANDOM_SALT_LENGTH
public static final short RANDOM_SALT_LENGTH
A constant indicating the size of the random salt that will be used if no salt is provided- See Also:
- Constant Field Values
-
-
Method Detail
-
setSalt
public abstract void setSalt(byte[] salt, short saltIndex, short saltLength)
Sets the salt value for the algorithm. If set to null, will generate a new salt automatically when callingderiveKey()
. The new salt will be available usinggetSalt()
.- Parameters:
salt
- The buffer for the saltsaltIndex
- The offset in the salt arraysaltLength
- The length of the salt array
-
getSalt
public abstract short getSalt(byte[] salt, short saltIndex) throws CryptoException
Returns the currently used salt buffer. If salt was not set, will return the randomly generated salt afterderiveKey()
is called.- Parameters:
salt
- The buffer for the salt array.saltIndex
- The offset in the salt array- Returns:
- Returns the length of the salt buffer
- Throws:
NotInitializedException
- if the salt is not initialized (setSalt()
was not called or set to null, andderiveKey()
was not called).CryptoException
- if some other error occurred
-
setHashAlgorithm
public abstract void setHashAlgorithm(short hashAlg) throws CryptoException
Sets the hash algorithm to be used for key derivation. If this method is not called, the default isHASH_TYPE_SHA256
.- Parameters:
hashAlg
- Can beHASH_TYPE_SHA1
orHASH_TYPE_SHA256
.- Throws:
IllegalParameterException
- if the hashAlg parameter is incorrectCryptoException
- if some other error occurred
-
getHashAlgorithm
public abstract short getHashAlgorithm()
Returns the currently used hash algorithm.- Returns:
- The currently used hash algorithm.
-
setIterationCount
public abstract void setIterationCount(int count) throws CryptoException
Sets the number of iterations the algorithm needs to run. Must be larger than 0- Parameters:
count
- The number of iterations the algorithm needs to run- Throws:
IllegalParameterException
- if the iteration count is not larger than 0, or if the iteration count is bigger than the value returned bygetIterationMaxCount()
.CryptoException
- if some other error occurred.
-
getIterationCount
public abstract int getIterationCount() throws CryptoException
Returns the current number of iterations the algorithm runs- Returns:
- The current number of iterations the algorithm runs
- Throws:
NotInitializedException
- ifsetIterationCount()
was not calledCryptoException
- if some other error occurred
-
getSaltSize
public abstract short getSaltSize() throws CryptoException
Returns the currently salt size- Returns:
- The currently salt size
- Throws:
CryptoException
-
deriveKey
public abstract void deriveKey(byte[] password, short passwordIndex, short passwordLength, byte[] key, short keyIndex, short keyLength) throws CryptoException
Creates a key based on the password provided. The key derivation will use the iteration count set insetIterationCount()
. This method must be called once prior to the key derivation. The key derivation will use the salt provided bysetSalt()
or a random salt with a length ofRANDOM_SALT_LENGTH
otherwise. The key derivation will use the hash algorithm set bysetHashAlgorithm()
or the defaultHASH_TYPE_SHA256
if not called.- Parameters:
password
- The password buffer to be used. Cannot be null.passwordIndex
- The offset in the password arraypasswordLength
- The length of the password array. Must not be larger thanPASSWORD_MAX_LENGTH
and bigger than zero.key
- The key buffer to contain the derived key. Cannot be null.keyIndex
- The offset in the key arraykeyLength
- The length of the key array. Must be bigger than zero.- Throws:
NotInitializedException
- ifsetIterationCount()
was not called.IllegalParameterException
- if one or more of the parameters were illegal.CryptoException
- if some other error occurred
-
getIterationMaxCount
public static int getIterationMaxCount() throws CryptoException
Returns maximum iteration count- Returns:
- The maximum iteration count
- Throws:
CryptoException
-
create
public static PasswordKeyDerivationAlg create()
Returns an instance of the PasswordKeyDerivationAlg class- Returns:
- An instance of the PasswordKeyDerivationAlg class
-
-