Coverage Report - org.acegisecurity.providers.ldap.authenticator.LdapShaPasswordEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
LdapShaPasswordEncoder
94% 
100% 
2.167
 
 1  
 /* Copyright 2004, 2005, 2006 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.ldap.authenticator;
 17  
 
 18  
 import org.acegisecurity.ldap.LdapDataAccessException;
 19  
 
 20  
 import org.acegisecurity.providers.encoding.PasswordEncoder;
 21  
 import org.acegisecurity.providers.encoding.ShaPasswordEncoder;
 22  
 
 23  
 import org.apache.commons.codec.binary.Base64;
 24  
 
 25  
 import org.springframework.util.Assert;
 26  
 
 27  
 import java.security.MessageDigest;
 28  
 
 29  
 
 30  
 /**
 31  
  * A version of {@link ShaPasswordEncoder} which supports Ldap SHA and SSHA (salted-SHA) encodings. The values are
 32  
  * base-64 encoded and have the label "{SHA}" (or "{SSHA}") prepended to the encoded hash. These can be made lower-case
 33  
  * in the encoded password, if required, by setting the <tt>forceLowerCasePrefix</tt> property to true.
 34  
  *
 35  
  * @author Luke Taylor
 36  
  * @version $Id: LdapShaPasswordEncoder.java 1958 2007-08-27 16:23:14Z luke_t $
 37  
  */
 38  
 public class LdapShaPasswordEncoder implements PasswordEncoder {
 39  
     //~ Static fields/initializers =====================================================================================
 40  
 
 41  
     /** The number of bytes in a SHA hash */
 42  
     private static final int SHA_LENGTH = 20;
 43  
     private static final String SSHA_PREFIX = "{SSHA}";
 44  1
     private static final String SSHA_PREFIX_LC = SSHA_PREFIX.toLowerCase();
 45  
     private static final String SHA_PREFIX = "{SHA}";
 46  1
     private static final String SHA_PREFIX_LC = SHA_PREFIX.toLowerCase();
 47  
 
 48  
     //~ Instance fields ================================================================================================
 49  
     private boolean forceLowerCasePrefix;
 50  
 
 51  
     //~ Constructors ===================================================================================================
 52  
 
 53  19
     public LdapShaPasswordEncoder() {}
 54  
 
 55  
     //~ Methods ========================================================================================================
 56  
 
 57  
     private byte[] combineHashAndSalt(byte[] hash, byte[] salt) {
 58  18
         if (salt == null) {
 59  10
             return hash;
 60  
         }
 61  
 
 62  8
         byte[] hashAndSalt = new byte[hash.length + salt.length];
 63  8
         System.arraycopy(hash, 0, hashAndSalt, 0, hash.length);
 64  8
         System.arraycopy(salt, 0, hashAndSalt, hash.length, salt.length);
 65  
 
 66  8
         return hashAndSalt;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Calculates the hash of password (and salt bytes, if supplied) and returns a base64 encoded concatenation
 71  
      * of the hash and salt, prefixed with {SHA} (or {SSHA} if salt was used).
 72  
      *
 73  
      * @param rawPass the password to be encoded.
 74  
      * @param salt the salt. Must be a byte array or null.
 75  
      *
 76  
      * @return the encoded password in the specified format
 77  
      *
 78  
      */
 79  
     public String encodePassword(String rawPass, Object salt) {
 80  
         MessageDigest sha;
 81  
 
 82  
         try {
 83  19
             sha = MessageDigest.getInstance("SHA");
 84  0
         } catch (java.security.NoSuchAlgorithmException e) {
 85  0
             throw new LdapDataAccessException("No SHA implementation available!", e);
 86  19
         }
 87  
 
 88  19
         sha.update(rawPass.getBytes());
 89  
 
 90  19
         if (salt != null) {
 91  10
             Assert.isInstanceOf(byte[].class, salt, "Salt value must be a byte array");
 92  8
             sha.update((byte[]) salt);
 93  
         }
 94  
 
 95  18
         byte[] hash = combineHashAndSalt(sha.digest(), (byte[]) salt);
 96  
 
 97  
         String prefix;
 98  
 
 99  18
         if (salt == null) {
 100  10
             prefix = forceLowerCasePrefix ? SHA_PREFIX_LC : SHA_PREFIX;
 101  
         } else {
 102  8
             prefix = forceLowerCasePrefix ? SSHA_PREFIX_LC : SSHA_PREFIX;
 103  
         }
 104  
 
 105  18
         return prefix + new String(Base64.encodeBase64(hash));
 106  
     }
 107  
 
 108  
     private byte[] extractSalt(String encPass) {
 109  6
         String encPassNoLabel = encPass.substring(6);
 110  
 
 111  6
         byte[] hashAndSalt = Base64.decodeBase64(encPassNoLabel.getBytes());
 112  6
         int saltLength = hashAndSalt.length - SHA_LENGTH;
 113  6
         byte[] salt = new byte[saltLength];
 114  6
         System.arraycopy(hashAndSalt, SHA_LENGTH, salt, 0, saltLength);
 115  
 
 116  6
         return salt;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Checks the validity of an unencoded password against an encoded one in the form
 121  
      * "{SSHA}sQuQF8vj8Eg2Y1hPdh3bkQhCKQBgjhQI".
 122  
      *
 123  
      * @param encPass the actual SSHA or SHA encoded password
 124  
      * @param rawPass unencoded password to be verified.
 125  
      * @param salt ignored. If the format is SSHA the salt bytes will be extracted from the encoded password.
 126  
      *
 127  
      * @return true if they match (independent of the case of the prefix).
 128  
      */
 129  
     public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
 130  
         String encPassWithoutPrefix;
 131  
 
 132  13
         if (encPass.startsWith(SSHA_PREFIX) || encPass.startsWith(SSHA_PREFIX_LC)) {
 133  6
             encPassWithoutPrefix = encPass.substring(6);
 134  6
             salt = extractSalt(encPass);
 135  
         } else {
 136  7
             encPassWithoutPrefix = encPass.substring(5);
 137  7
             salt = null;
 138  
         }
 139  
 
 140  
         // Compare the encoded passwords without the prefix
 141  13
         return encodePassword(rawPass, salt).endsWith(encPassWithoutPrefix);
 142  
     }
 143  
 
 144  
     public void setForceLowerCasePrefix(boolean forceLowerCasePrefix) {
 145  6
         this.forceLowerCasePrefix = forceLowerCasePrefix;
 146  6
     }
 147  
 }