Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.util.security;
 
 
 
 /* ------------------------------------------------------------ */
Credentials. The Credential class represents an abstract mechanism for checking authentication credentials. A credential instance either represents a secret, or some data that could only be derived from knowing the secret.

Often a Credential is related to a Password via a one way algorithm, so while a Password itself is a Credential, a UnixCrypt or MD5 digest of a a password is only a credential that can be checked against the password.

This class includes an implementation for unix Crypt an MD5 digest.

See also:
Password
 
 public abstract class Credential implements Serializable
 {
     private static final Logger LOG = Log.getLogger(Credential.class);
 
     private static final long serialVersionUID = -7760551052768181572L;
 
     /* ------------------------------------------------------------ */
    
Check a credential

Parameters:
credentials The credential to check against. This may either be another Credential object, a Password object or a String which is interpreted by this credential.
Returns:
True if the credentials indicated that the shared secret is known to both this Credential and the passed credential.
 
     public abstract boolean check(Object credentials);
 
     /* ------------------------------------------------------------ */
    
Get a credential from a String. If the credential String starts with a known Credential type (eg "CRYPT:" or "MD5:" ) then a Credential of that type is returned. Else the credential is assumed to be a Password.

Parameters:
credential String representation of the credential
Returns:
A Credential or Password instance.
 
     public static Credential getCredential(String credential)
     {
         if (credential.startsWith(.)) return new Crypt(credential);
         if (credential.startsWith(.)) return new MD5(credential);
 
         return new Password(credential);
     }
 
     /* ------------------------------------------------------------ */
    
Unix Crypt Credentials
 
     public static class Crypt extends Credential
     {
         private static final long serialVersionUID = -2027792997664744210L;
 
         public static final String __TYPE = "CRYPT:";
 
         private final String _cooked;
 
         Crypt(String cooked)
         {
              = cooked.startsWith(.) ? cooked.substring(.length()) : cooked;
         }
 
         @Override
         public boolean check(Object credentials)
         {
             if (credentials instanceof char[])
                credentials=new String((char[])credentials);
            if (!(credentials instanceof String) && !(credentials instanceof Password)) 
                .warn("Can't check " + credentials.getClass() + " against CRYPT");
            String passwd = credentials.toString();
            return .equals(UnixCrypt.crypt(passwd));
        }
        public static String crypt(String userString pw)
        {
            return "CRYPT:" + UnixCrypt.crypt(pwuser);
        }
    }
    /* ------------------------------------------------------------ */
    
MD5 Credentials
    public static class MD5 extends Credential
    {
        private static final long serialVersionUID = 5533846540822684240L;
        public static final String __TYPE = "MD5:";
        public static final Object __md5Lock = new Object();
        private static MessageDigest __md;
        private final byte[] _digest;
        /* ------------------------------------------------------------ */
        MD5(String digest)
        {
            digest = digest.startsWith() ? digest.substring(.length()) : digest;
             = TypeUtil.parseBytes(digest, 16);
        }
        /* ------------------------------------------------------------ */
        public byte[] getDigest()
        {
            return ;
        }
        /* ------------------------------------------------------------ */
        @Override
        public boolean check(Object credentials)
        {
            try
            {
                byte[] digest = null;
                if (credentials instanceof char[])
                    credentials=new String((char[])credentials);
                if (credentials instanceof Password || credentials instanceof String)
                {
                    synchronized ()
                    {
                        if ( == null = MessageDigest.getInstance("MD5");
                        .reset();
                        .update(credentials.toString().getBytes(.));
                        digest = .digest();
                    }
                    if (digest == null || digest.length != .return false;
                    for (int i = 0; i < digest.lengthi++)
                        if (digest[i] != [i]) return false;
                    return true;
                }
                else if (credentials instanceof MD5)
                {
                    MD5 md5 = (MD5credentials;
                    if (. != md5._digest.lengthreturn false;
                    for (int i = 0; i < .i++)
                        if ([i] != md5._digest[i]) return false;
                    return true;
                }
                else if (credentials instanceof Credential)
                {
                    // Allow credential to attempt check - i.e. this'll work
                    // for DigestAuthModule$Digest credentials
                    return ((Credentialcredentials).check(this);
                }
                else
                {
                    .warn("Can't check " + credentials.getClass() + " against MD5");
                    return false;
                }
            }
            catch (Exception e)
            {
                .warn(e);
                return false;
            }
        }
        /* ------------------------------------------------------------ */
        public static String digest(String password)
        {
            try
            {
                byte[] digest;
                synchronized ()
                {
                    if ( == null)
                    {
                        try
                        {
                             = MessageDigest.getInstance("MD5");
                        }
                        catch (Exception e)
                        {
                            .warn(e);
                            return null;
                        }
                    }
                    .reset();
                    .update(password.getBytes(.));
                    digest = .digest();
                }
                return  + TypeUtil.toString(digest, 16);
            }
            catch (Exception e)
            {
                .warn(e);
                return null;
            }
        }
    }
New to GrepCode? Check out our FAQ X