Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.appbasics.security.password.hash;
 
 import java.util.Map;
 import java.util.Set;
 
 
 
This class manages multiple IPasswordHashCreator instances

Author(s):
Philip Helger
 
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (PasswordHashCreatorManager.class);
 
   private final ReadWriteLock m_aRWLock = new ReentrantReadWriteLock ();
   @GuardedBy ("m_aRWLock")
   @GuardedBy ("m_aRWLock")
 
   {}

  
Register a new password hash creator. No other password hash creator with the same algorithm name may be registered.

Parameters:
aPasswordHashCreator The password hash creator to be registered. May not be null.
 
   public void registerPasswordHashCreator (@Nonnull final IPasswordHashCreator aPasswordHashCreator)
   {
     ValueEnforcer.notNull (aPasswordHashCreator"PasswordHashCreator");
 
     final String sAlgorithmName = aPasswordHashCreator.getAlgorithmName ();
     if (StringHelper.hasNoText (sAlgorithmName))
       throw new IllegalArgumentException ("PasswordHashCreator algorithm '" + aPasswordHashCreator + "' is empty!");
 
     .writeLock ().lock ();
     try
     {
       if (.containsKey (sAlgorithmName))
         throw new IllegalArgumentException ("Another PasswordHashCreator for algorithm '" +
                                             sAlgorithmName +
                                             "' is already registered!");
       .put (sAlgorithmNameaPasswordHashCreator);
     }
     finally
     {
       .writeLock ().unlock ();
     }
     .info ("Registered password hash creator algorithm '" + sAlgorithmName + "' to " + aPasswordHashCreator);
   }
 
   public void unregisterPasswordHashCreator (@Nullable final IPasswordHashCreator aPasswordHashCreator)
   {
     if (aPasswordHashCreator != null)
     {
       final String sAlgorithmName = aPasswordHashCreator.getAlgorithmName ();
       if (StringHelper.hasText (sAlgorithmName))
       {
         .writeLock ().lock ();
        try
        {
          if (.remove (sAlgorithmName) != null)
            .info ("Unregistered password hash creator algorithm '" + sAlgorithmName + "'");
        }
        finally
        {
          .writeLock ().unlock ();
        }
      }
    }
  }

  
Get the password hash creator of the specified algorithm name.

Parameters:
sAlgorithmName The algorithm name to query. May be null.
Returns:
null if no such hash creator is registered.
  {
    .readLock ().lock ();
    try
    {
      return .get (sAlgorithmName);
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  {
    .readLock ().lock ();
    try
    {
      return ContainerHelper.newSet (.keySet ());
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  {
    .readLock ().lock ();
    try
    {
      return ContainerHelper.newList (.values ());
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  
Set the default password hash creator algorithm. A matching IPasswordHashCreator object must be registered previously using registerPasswordHashCreator(com.helger.appbasics.security.password.hash.IPasswordHashCreator).

Parameters:
sAlgorithm The name of the algorithm to use as the default. May neither be null nor empty.
  public void setDefaultPasswordHashCreatorAlgorithm (@Nonnull @Nonempty final String sAlgorithm)
  {
    ValueEnforcer.notEmpty (sAlgorithm"Algorithm");
    .writeLock ().lock ();
    try
    {
      final IPasswordHashCreator aPHC = .get (sAlgorithm);
      if (aPHC == null)
        throw new IllegalArgumentException ("No PasswordHashCreator registered for algorithm '" + sAlgorithm + "'");
       = aPHC;
    }
    finally
    {
      .writeLock ().unlock ();
    }
    .info ("Default PasswordHashCreator algorithm set to '" + sAlgorithm + "'");
  }

  

Returns:
The default IPasswordHashCreator algorithm to use. Never null.
  {
    .readLock ().lock ();
    try
    {
      if (ret == null)
        throw new IllegalStateException ("No default PasswordHashCreator present!");
      return ret;
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  

Returns:
The default password hash creator algorithm name currently in use.
  {
  }

  
Create the password hash from the passed plain text password, using the default password hash creator.

Parameters:
sPlainTextPassword Plain text password. May not be null.
Returns:
The password hash. Never null.
See also:
getDefaultPasswordHashCreator()
  public PasswordHash createUserDefaultPasswordHash (@Nonnull final String sPlainTextPassword)
  {
    ValueEnforcer.notNull (sPlainTextPassword"PlainTextPassword");
    final String sPasswordHash = aPHC.createPasswordHash (sPlainTextPassword);
    return new PasswordHash (aPHC.getAlgorithmName (), sPasswordHash);
  }

  
Create the password hash from the passed plain text password, using the default password hash creator.

Parameters:
sAlgorithmName The password hash creator algorithm name to query. May neither be null nor empty.
sPlainTextPassword Plain text password. May not be null.
Returns:
The password hash. Never null.
See also:
getDefaultPasswordHashCreator()
  public PasswordHash createUserPasswordHash (@Nonnull @Nonempty final String sAlgorithmName,
                                              @Nonnull final String sPlainTextPassword)
  {
    ValueEnforcer.notNull (sPlainTextPassword"PlainTextPassword");
    final IPasswordHashCreator aPHC = getPasswordHashCreatorOfAlgorithm (sAlgorithmName);
    if (aPHC == null)
      throw new IllegalArgumentException ("No password hash creator for algorithm '" + sAlgorithmName + "' registered!");
    final String sPasswordHash = aPHC.createPasswordHash (sPlainTextPassword);
    return new PasswordHash (sAlgorithmNamesPasswordHash);
  }
  public String toString ()
  {
    return new ToStringGenerator (this).append ("map")
                                       .append ("default")
                                       .toString ();
  }
New to GrepCode? Check out our FAQ X