Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you 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 org.apache.felix.webconsole.internal.servlet;
 
 
The Password class encapsulates encoding and decoding operations on plain text and hashed passwords.

Encoded hashed passwords are strings of the form {hashAlgorithm}base64-encoded-password-hash where hashAlgorithm is the name of the hash algorithm used to hash the password and base64-encoded-password-hash is the password hashed with the indicated hash algorithm and subsequently encoded in Base64.

 
 class Password
 {
 
     // the default hash algorithm (part of the Java Platform since 1.4)
     private static final String DEFAULT_HASH_ALGO = "SHA-256";
 
     // the hash algorithm used to hash the password or null
     // if the password is not hashed at all
     private final String hashAlgo;
 
     // the hashed or plain password
     private final byte[] password;


    
Returns true if the given textPassword is hashed and encoded as described in the class comment.

Parameters:
textPassword
Returns:
Throws:
java.lang.NullPointerException if textPassword is null.
 
     static boolean isPasswordHashedfinal String textPassword )
     {
         return getEndOfHashAlgorithmtextPassword ) >= 0;
     }


    
Returns the given plain textPassword as an encoded hashed password string as described in the class comment.

Parameters:
textPassword
Returns:
Throws:
java.lang.NullPointerException if textPassword is null.
 
     static String hashPasswordfinal String textPassword )
     {
         final byte[] bytePassword = Base64.getBytesUtf8textPassword );
         return hashPasswordbytePassword );
     }
 
 
     PasswordString textPassword )
     {
         this. = getPasswordHashAlgorithmtextPassword );
         this. = getPasswordBytestextPassword );
     }


    
Returns true if this password matches the password toCompare. If this password is hashed, the toCompare password is hashed, too, with the same hash algorithm before comparison.

Parameters:
toCompare
Returns:
Throws:
java.lang.NullPointerException if toCompare is null.
 
     boolean matchesfinal byte[] toCompare )
     {
        return Arrays.equalsthis.hashPasswordtoComparethis. ) );
    }


    
Returns this password as a string hashed and encoded as described by the class comment. If this password has not been hashed originally, the default hash algorithm SHA-256 is applied.
    public String toString()
    {
        return hashPasswordthis.this. );
    }
    private static String hashPasswordfinal String hashAlgorithmfinal byte[] password )
    {
        final String actualHashAlgo = ( hashAlgorithm == null ) ?  : hashAlgorithm;
        final byte[] hashedPassword = hashPasswordpasswordactualHashAlgo );
        final StringBuffer buf = new StringBuffer( 2 + actualHashAlgo.length() + hashedPassword.length * 3 );
        buf.append'{' ).appendactualHashAlgo.toLowerCase() ).append'}' );
        buf.append( Base64.newStringUtf8( Base64.encodeBase64hashedPassword ) ) );
        return buf.toString();
    }
    private static String getPasswordHashAlgorithmfinal String textPassword )
    {
        final int endHash = getEndOfHashAlgorithmtextPassword );
        if ( endHash >= 0 )
        {
            return textPassword.substring( 1, endHash );
        }
        // password is plain text, hence no algorithm
        return null;
    }
    private static byte[] getPasswordBytesfinal String textPassword )
    {
        final int endHash = getEndOfHashAlgorithmtextPassword );
        if ( endHash >= 0 )
        {
            final String encodedPassword = textPassword.substringendHash + 1 );
            return Base64.decodeBase64encodedPassword );
        }
        return Base64.getBytesUtf8textPassword );
    }
    private static int getEndOfHashAlgorithmfinal String textPassword )
    {
        if ( textPassword.startsWith"{" ) )
        {
            final int endHash = textPassword.indexOf"}" );
            if ( endHash > 0 )
            {
                return endHash;
            }
        }
        return -1;
    }
    private static byte[] hashPasswordfinal byte[] pwdfinal String hashAlg )
    {
        // no hashing if no hash algorithm
        if ( hashAlg == null || hashAlg.length() == 0 )
        {
            return pwd;
        }
        try
        {
            final MessageDigest md = MessageDigest.getInstancehashAlg );
            return md.digestpwd );
        }
        catch ( NoSuchAlgorithmException e )
        {
            throw new IllegalStateException"Cannot hash the password: " + e );
        }
    }
New to GrepCode? Check out our FAQ X