Package org.wildfly.security.password
Class PasswordFactory
- java.lang.Object
-
- org.wildfly.security.password.PasswordFactory
-
public final class PasswordFactory extends Object
A factory for passwords.Password factories are used to handle and manipulate password objects and their corresponding password specifications. Passwords are a kind of key which are used to store and compare against a string of text entered by a human. Passwords can be one-way (
OneWayPassword
) or two-way (TwoWayPassword
).- Author:
- David M. Lloyd
-
-
Constructor Summary
Constructors Constructor Description PasswordFactory(PasswordFactorySpi spi, Provider provider, String algorithm)
Construct a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends KeySpec>
booleanconvertibleToKeySpec(Password password, Class<T> specType)
Determine whether the given password can be converted to the given key specification type by this factory.Password
generatePassword(KeySpec keySpec)
Generate a newPassword
object for the given specification.String
getAlgorithm()
Get the algorithm of this password factory.static PasswordFactory
getInstance(String algorithm)
Get a password factory instance.static PasswordFactory
getInstance(String algorithm, String providerName)
Get a password factory instance.static PasswordFactory
getInstance(String algorithm, Provider provider)
Get a password factory instance.static PasswordFactory
getInstance(String algorithm, Supplier<Provider[]> providerSupplier)
Get a password factory instance.<T extends KeySpec>
TgetKeySpec(Password password, Class<T> specType)
Generate a key specification of the given type from the given password object.Provider
getProvider()
Get the provider of this password factory.boolean
isTranslatable(Password password)
Determine whether the given password can be translated into one which is consumable by this factory.Password
transform(Password password, AlgorithmParameterSpec parameterSpec)
Transform a password with new parameters.Password
translate(Password password)
Translate the given password object to one which is consumable by this factory.boolean
verify(Password password, char[] guess)
Verify a password guess.
-
-
-
Constructor Detail
-
PasswordFactory
public PasswordFactory(PasswordFactorySpi spi, Provider provider, String algorithm)
Construct a new instance.- Parameters:
spi
- the password factory SPI (notnull
)provider
- the provider (notnull
)algorithm
- the algorithm name (notnull
)
-
-
Method Detail
-
getInstance
public static PasswordFactory getInstance(String algorithm) throws NoSuchAlgorithmException
Get a password factory instance. The returned password factory object will implement the given algorithm.- Parameters:
algorithm
- the name of the algorithm- Returns:
- a password factory instance
- Throws:
NoSuchAlgorithmException
- if the given algorithm has no available implementations
-
getInstance
public static PasswordFactory getInstance(String algorithm, String providerName) throws NoSuchAlgorithmException, NoSuchProviderException
Get a password factory instance. The returned password factory object will implement the given algorithm.- Parameters:
algorithm
- the name of the algorithmproviderName
- the name of the provider to use- Returns:
- a password factory instance
- Throws:
NoSuchAlgorithmException
- if the given algorithm has no available implementationsNoSuchProviderException
-
getInstance
public static PasswordFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Get a password factory instance. The returned password factory object will implement the given algorithm.- Parameters:
algorithm
- the name of the algorithmprovider
- the provider to use- Returns:
- a password factory instance
- Throws:
NoSuchAlgorithmException
- if the given algorithm has no available implementations
-
getInstance
public static PasswordFactory getInstance(String algorithm, Supplier<Provider[]> providerSupplier) throws NoSuchAlgorithmException
Get a password factory instance. The returned password factory object will implement the given algorithm.- Parameters:
algorithm
- the name of the algorithmproviderSupplier
- the provider supplier to search- Returns:
- a password factory instance
- Throws:
NoSuchAlgorithmException
- if the given algorithm has no available implementations
-
getProvider
public Provider getProvider()
Get the provider of this password factory.- Returns:
- the provider
-
getAlgorithm
public String getAlgorithm()
Get the algorithm of this password factory.- Returns:
- the algorithm
-
generatePassword
public Password generatePassword(KeySpec keySpec) throws InvalidKeySpecException
Generate a newPassword
object for the given specification.- Parameters:
keySpec
- the specification- Returns:
- the password object
- Throws:
InvalidKeySpecException
- if the key specification is not valid for this algorithm
-
getKeySpec
public <T extends KeySpec> T getKeySpec(Password password, Class<T> specType) throws InvalidKeySpecException
Generate a key specification of the given type from the given password object.- Type Parameters:
T
- the specification type- Parameters:
password
- the password objectspecType
- the specification class- Returns:
- the key specification
- Throws:
InvalidKeySpecException
- if the password cannot be translated to the given key specification type
-
convertibleToKeySpec
public <T extends KeySpec> boolean convertibleToKeySpec(Password password, Class<T> specType)
Determine whether the given password can be converted to the given key specification type by this factory.- Type Parameters:
T
- the specification type- Parameters:
password
- the password objectspecType
- the specification class- Returns:
true
if the password can be converted,false
otherwise
-
isTranslatable
public boolean isTranslatable(Password password)
Determine whether the given password can be translated into one which is consumable by this factory. If this method returnstrue
, thentranslate(Password)
will succeed.- Parameters:
password
- the password object- Returns:
true
if the given password is supported by this algorithm,false
otherwise
-
translate
public Password translate(Password password) throws InvalidKeyException
Translate the given password object to one which is consumable by this factory.- Parameters:
password
- the password object- Returns:
- the equivalent password object that this factory can work with
- Throws:
InvalidKeyException
- if the given password is not supported by this algorithm
-
verify
public boolean verify(Password password, char[] guess) throws InvalidKeyException
Verify a password guess.- Parameters:
password
- the password objectguess
- the guessed password characters- Returns:
true
if the guess matches the password,false
otherwise- Throws:
InvalidKeyException
- if the given password is not supported by this factory
-
transform
public Password transform(Password password, AlgorithmParameterSpec parameterSpec) throws InvalidKeyException, InvalidAlgorithmParameterException
Transform a password with new parameters. Not every transformation is allowed, but iterative password types generally should allow increasing the number of iterations.- Parameters:
password
- the passwordparameterSpec
- the new parameters- Returns:
- the transformed password
- Throws:
InvalidKeyException
- if the given password is invalidInvalidAlgorithmParameterException
- if the transformation cannot be applied to the given parameters
-
-