org.apache.shiro.authc.credential
Class DefaultPasswordService

java.lang.Object
  extended by org.apache.shiro.authc.credential.DefaultPasswordService
All Implemented Interfaces:
HashingPasswordService, PasswordService

public class DefaultPasswordService
extends Object
implements HashingPasswordService

Default implementation of the PasswordService interface that relies on an internal HashService, HashFormat, and HashFormatFactory to function:

Hashing Passwords

Comparing Passwords

All hashing operations are performed by the internal hashService. After the hash is computed, it is formatted into a String value via the internal hashFormat.

Since:
1.2

Field Summary
static String DEFAULT_HASH_ALGORITHM
           
static int DEFAULT_HASH_ITERATIONS
           
 
Constructor Summary
DefaultPasswordService()
           
 
Method Summary
protected  HashRequest buildHashRequest(ByteSource plaintext, Hash saved)
           
protected  void checkHashFormatDurability()
           
protected  ByteSource createByteSource(Object o)
           
protected  HashRequest createHashRequest(ByteSource plaintext)
           
 String encryptPassword(Object plaintext)
          Converts the specified plaintext password (usually acquired from your application's 'new user' or 'password reset' workflow) into a formatted string safe for storage.
 HashFormat getHashFormat()
           
 HashFormatFactory getHashFormatFactory()
           
 HashService getHashService()
           
 Hash hashPassword(Object plaintext)
          Hashes the specified plaintext password using internal hashing configuration settings pertinent to password hashing.
 boolean passwordsMatch(Object plaintext, Hash saved)
          Returns true if the submittedPlaintext password matches the existing savedPasswordHash, false otherwise.
 boolean passwordsMatch(Object submittedPlaintext, String saved)
          Returns true if the submittedPlaintext password matches the existing saved password, false otherwise.
 void setHashFormat(HashFormat hashFormat)
           
 void setHashFormatFactory(HashFormatFactory hashFormatFactory)
           
 void setHashService(HashService hashService)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_HASH_ALGORITHM

public static final String DEFAULT_HASH_ALGORITHM
See Also:
Constant Field Values

DEFAULT_HASH_ITERATIONS

public static final int DEFAULT_HASH_ITERATIONS
See Also:
Constant Field Values
Constructor Detail

DefaultPasswordService

public DefaultPasswordService()
Method Detail

encryptPassword

public String encryptPassword(Object plaintext)
Description copied from interface: PasswordService
Converts the specified plaintext password (usually acquired from your application's 'new user' or 'password reset' workflow) into a formatted string safe for storage. The returned string can be safely saved with the corresponding user account record (e.g. as a 'password' attribute).

It is expected that the String returned from this method will be presented to the passwordsMatch(plaintext,encrypted) method when performing a password comparison check.

Usage

The input argument type can be any 'byte backed' Object - almost always either a String or character array representing passwords (character arrays are often a safer way to represent passwords as they can be cleared/nulled-out after use. Any argument type supported by ByteSource.Util#isCompatible(Object) is valid.

For example:

 String rawPassword = ...
 String encryptedValue = passwordService.encryptPassword(rawPassword);
 
or, identically:
 char[] rawPasswordChars = ...
 String encryptedValue = passwordService.encryptPassword(rawPasswordChars);
 

The resulting encryptedValue should be stored with the account to be retrieved later during a login attempt. For example:

 String encryptedValue = passwordService.encryptPassword(rawPassword);
 ...
 userAccount.setPassword(encryptedValue);
 userAccount.save(); //create or update to your data store
 

Specified by:
encryptPassword in interface PasswordService
Parameters:
plaintext - the raw password as 'byte-backed' object (String, character array, ByteSource, etc) usually acquired from your application's 'new user' or 'password reset' workflow.
Returns:
the encrypted password, formatted for storage.
See Also:
ByteSource.Util#isCompatible(Object)

hashPassword

public Hash hashPassword(Object plaintext)
Description copied from interface: HashingPasswordService
Hashes the specified plaintext password using internal hashing configuration settings pertinent to password hashing.

Note that this method is only likely to be used in more complex environments that wish to format and/or save the returned Hash object in a custom manner. Most applications will find the encryptPassword method suitable enough for safety and ease-of-use.

Usage

The input argument type can be any 'byte backed' Object - almost always either a String or character array representing passwords (character arrays are often a safer way to represent passwords as they can be cleared/nulled-out after use. Any argument type supported by ByteSource.Util#isCompatible(Object) is valid.

Regardless of your choice of using Strings or character arrays to represent submitted passwords, you can wrap either as a ByteSource by using ByteSource.Util, for example, when the passwords are captured as Strings:

 ByteSource passwordBytes = ByteSource.Util.bytes(submittedPasswordString);
 Hash hashedPassword = hashingPasswordService.hashPassword(passwordBytes);
 
or, identically, when captured as a character array:
 ByteSource passwordBytes = ByteSource.Util.bytes(submittedPasswordCharacterArray);
 Hash hashedPassword = hashingPasswordService.hashPassword(passwordBytes);
 

Specified by:
hashPassword in interface HashingPasswordService
Parameters:
plaintext - the raw password as 'byte-backed' object (String, character array, ByteSource, etc) usually acquired from your application's 'new user' or 'password reset' workflow.
Returns:
the hashed password.
See Also:
ByteSource.Util#isCompatible(Object), PasswordService.encryptPassword(Object)

passwordsMatch

public boolean passwordsMatch(Object plaintext,
                              Hash saved)
Description copied from interface: HashingPasswordService
Returns true if the submittedPlaintext password matches the existing savedPasswordHash, false otherwise. Note that this method is only likely to be used in more complex environments that save hashes in a custom manner. Most applications will find the passwordsMatch(plaintext,string) method sufficient if encrypting passwords as Strings.

Usage

The submittedPlaintext argument type can be any 'byte backed' Object - almost always either a String or character array representing passwords (character arrays are often a safer way to represent passwords as they can be cleared/nulled-out after use. Any argument type supported by ByteSource.Util#isCompatible(Object) is valid.

Specified by:
passwordsMatch in interface HashingPasswordService
Parameters:
plaintext - a raw/plaintext password submitted by an end user/Subject.
saved - the previously hashed password known to be associated with an account. This value is expected to have been previously generated from the hashPassword method (typically when the account is created or the account's password is reset).
Returns:
true if the plaintext password matches the existing savedPasswordHash, false otherwise.

checkHashFormatDurability

protected void checkHashFormatDurability()

createHashRequest

protected HashRequest createHashRequest(ByteSource plaintext)

createByteSource

protected ByteSource createByteSource(Object o)

passwordsMatch

public boolean passwordsMatch(Object submittedPlaintext,
                              String saved)
Description copied from interface: PasswordService
Returns true if the submittedPlaintext password matches the existing saved password, false otherwise.

Usage

The submittedPlaintext argument type can be any 'byte backed' Object - almost always either a String or character array representing passwords (character arrays are often a safer way to represent passwords as they can be cleared/nulled-out after use. Any argument type supported by ByteSource.Util#isCompatible(Object) is valid.

For example:

 String submittedPassword = ...
 passwordService.passwordsMatch(submittedPassword, encryptedPassword);
 
or similarly:
 char[] submittedPasswordCharacters = ...
 passwordService.passwordsMatch(submittedPasswordCharacters, encryptedPassword);
 

Specified by:
passwordsMatch in interface PasswordService
Parameters:
submittedPlaintext - a raw/plaintext password submitted by an end user/Subject.
saved - the previously encrypted password known to be associated with an account. This value is expected to have been previously generated from the encryptPassword method (typically when the account is created or the account's password is reset).
Returns:
true if the submittedPlaintext password matches the existing saved password, false otherwise.
See Also:
ByteSource.Util#isCompatible(Object)

buildHashRequest

protected HashRequest buildHashRequest(ByteSource plaintext,
                                       Hash saved)

getHashService

public HashService getHashService()

setHashService

public void setHashService(HashService hashService)

getHashFormat

public HashFormat getHashFormat()

setHashFormat

public void setHashFormat(HashFormat hashFormat)

getHashFormatFactory

public HashFormatFactory getHashFormatFactory()

setHashFormatFactory

public void setHashFormatFactory(HashFormatFactory hashFormatFactory)


Copyright © 2004-2014 The Apache Software Foundation. All Rights Reserved.