View Javadoc

    1   /* Copyright 2004 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.providers.encoding;
    17
    18  import org.apache.commons.codec.binary.Base64;
    19  import org.apache.commons.codec.digest.DigestUtils;
    20
    21
    22  /***
    23   * <p>
    24   * SHA implementation of PasswordEncoder.
    25   * </p>
    26   *
    27   * <p>
    28   * If a <code>null</code> password is presented, it will be treated as an empty
    29   * <code>String</code> ("") password.
    30   * </p>
    31   *
    32   * <P>
    33   * As SHA is a one-way hash, the salt can contain any characters.
    34   * </p>
    35   *
    36   * @author colin sampaleanu
    37   * @author Ben Alex
    38   * @version $Id: ShaPasswordEncoder.java,v 1.4 2005/11/17 00:55:49 benalex Exp $
    39   */
    40  public class ShaPasswordEncoder extends BaseDigestPasswordEncoder
    41      implements PasswordEncoder {
    42      //~ Methods ================================================================
    43
    44      public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
    45          String pass1 = "" + encPass;
    46          String pass2 = encodeInternal(mergePasswordAndSalt(rawPass, salt, false));
    47
    48          return pass1.equals(pass2);
    49      }
    50
    51      public String encodePassword(String rawPass, Object salt) {
    52          return encodeInternal(mergePasswordAndSalt(rawPass, salt, false));
    53      }
    54
    55      private String encodeInternal(String input) {
    56          if (!getEncodeHashAsBase64()) {
    57              return DigestUtils.shaHex(input);
    58          }
    59
    60          byte[] encoded = Base64.encodeBase64(DigestUtils.sha(input));
    61
    62          return new String(encoded);
    63      }
    64  }