directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pamarce...@apache.org
Subject svn commit: r1490334 - in /directory/studio/trunk/plugins: ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/model/ valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/
Date Thu, 06 Jun 2013 16:12:28 GMT
Author: pamarcelot
Date: Thu Jun  6 16:12:27 2013
New Revision: 1490334

URL: http://svn.apache.org/r1490334
Log:
Another fix for DIRSTUDIO-890 (Add support for SSHA2 in Password Editor).

Modified:
    directory/studio/trunk/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/model/Password.java
    directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordDialog.java
    directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordValueEditor.java
    directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages.properties
    directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_de.properties
    directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_fr.properties

Modified: directory/studio/trunk/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/model/Password.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/model/Password.java?rev=1490334&r1=1490333&r2=1490334&view=diff
==============================================================================
--- directory/studio/trunk/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/model/Password.java
(original)
+++ directory/studio/trunk/plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/model/Password.java
Thu Jun  6 16:12:27 2013
@@ -25,6 +25,7 @@ import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 
+import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants;
 import org.apache.directory.studio.ldapbrowser.core.BrowserCoreMessages;
 import org.apache.directory.studio.ldapbrowser.core.utils.UnixCrypt;
 import org.apache.directory.studio.ldifparser.LdifUtils;
@@ -40,6 +41,12 @@ import org.apache.directory.studio.ldifp
  * <ul>
  *   <li>SHA</li>
  *   <li>SSHA</li>
+ *   <li>SHA-256</li>
+ *   <li>SSHA-256</li>
+ *   <li>SHA-384</li>
+ *   <li>SSHA-384</li>
+ *   <li>SHA-512</li>
+ *   <li>SSHA-512</li>
  *   <li>MD5</li>
  *   <li>SMD5</li>
  *   <li>CRYPT</li>
@@ -49,57 +56,19 @@ import org.apache.directory.studio.ldifp
  */
 public class Password
 {
-    /** The constant used for the SHA hash, value <code>SHA</code> */
-    public static final String HASH_METHOD_SHA = "SHA"; //$NON-NLS-1$
+    /** The hash method */
+    private LdapSecurityConstants hashMethod;
 
-    /** The constant used for the salted SHA hash, value <code>SSHA</code> */
-    public static final String HASH_METHOD_SSHA = "SSHA"; //$NON-NLS-1$
-
-    /** The constant used for the SHA-256 hash, value <code>SHA-256</code> */
-    public static final String HASH_METHOD_SHA_256 = "SHA-256"; //$NON-NLS-1$
-
-    /** The constant used for the salted SHA-256 hash, value <code>SSHA-256</code>
*/
-    public static final String HASH_METHOD_SSHA_256 = "SSHA-256"; //$NON-NLS-1$
-
-    /** The constant used for the SHA-384 hash, value <code>SHA-384</code> */
-    public static final String HASH_METHOD_SHA_384 = "SHA-384"; //$NON-NLS-1$
-
-    /** The constant used for the salted SHA-384 hash, value <code>SSHA-384</code>
*/
-    public static final String HASH_METHOD_SSHA_384 = "SSHA-384"; //$NON-NLS-1$
-
-    /** The constant used for the SHA-512 hash, value <code>SHA-512</code> */
-    public static final String HASH_METHOD_SHA_512 = "SHA-512"; //$NON-NLS-1$
-
-    /** The constant used for the salted SHA-512 hash, value <code>SSHA-512</code>
*/
-    public static final String HASH_METHOD_SSHA_512 = "SSHA-512"; //$NON-NLS-1$
-
-    /** The constant used for the MD5 hash, value <code>MD5</code> */
-    public static final String HASH_METHOD_MD5 = "MD5"; //$NON-NLS-1$
-
-    /** The constant used for the salted MD5 hash, value <code>SMD5</code> */
-    public static final String HASH_METHOD_SMD5 = "SMD5"; //$NON-NLS-1$
-
-    /** The constant used for the crypt hash, value <code>CRYPT</code> */
-    public static final String HASH_METHOD_CRYPT = "CRYPT"; //$NON-NLS-1$
-
-    /** The constant used for plain text passwords */
-    public static final String HASH_METHOD_NO = BrowserCoreMessages.model__no_hash;
-
-    /** The constant used for unsupported hash methods */
-    public static final String HASH_METHOD_UNSUPPORTED = BrowserCoreMessages.model__unsupported_hash;
-
-    /** The constant used for invalid password hashes */
-    public static final String HASH_METHOD_INVALID = BrowserCoreMessages.model__invalid_hash;
-
-    /** The hash method. */
-    private String hashMethod;
-
-    /** The hashed password. */
+    /** The hashed password */
     private byte[] hashedPassword;
 
-    /** The salt. */
+    /** The salt */
     private byte[] salt;
 
+    private boolean isUnsupportedHashMethod = false;
+
+    private boolean isInvalidHashValue = false;
+
     /** The trash, used for unknown hash methods. */
     private String trash;
 
@@ -126,54 +95,58 @@ public class Password
         {
             throw new IllegalArgumentException( BrowserCoreMessages.model__empty_password
);
         }
-        else if ( password.indexOf( '{' ) == 0 && password.indexOf( '}' ) > 0
)
+        else if ( ( password.indexOf( '{' ) == 0 ) && ( password.indexOf( '}' ) >
0 ) )
         {
             try
             {
-                hashMethod = password.substring( password.indexOf( '{' ) + 1, password.indexOf(
'}' ) );
-                String rest = password.substring( hashMethod.length() + 2 );
-
-                if ( HASH_METHOD_SHA.equalsIgnoreCase( hashMethod )
-                    || HASH_METHOD_SHA_256.equalsIgnoreCase( hashMethod )
-                    || HASH_METHOD_SHA_384.equalsIgnoreCase( hashMethod )
-                    || HASH_METHOD_SHA_512.equalsIgnoreCase( hashMethod )
-                    || HASH_METHOD_MD5.equalsIgnoreCase( hashMethod ) )
+                // Getting the hash method
+                String hashMethodString = password.substring( password.indexOf( '{' ) + 1,
password.indexOf( '}' ) );
+                hashMethod = LdapSecurityConstants.getAlgorithm( hashMethodString );
+
+                // Getting the rest of the hashed password
+                String rest = password.substring( hashMethodString.length() + 2 );
+
+                if ( ( LdapSecurityConstants.HASH_METHOD_SHA == hashMethod )
+                    || ( LdapSecurityConstants.HASH_METHOD_SHA256 == hashMethod )
+                    || ( LdapSecurityConstants.HASH_METHOD_SHA384 == hashMethod )
+                    || ( LdapSecurityConstants.HASH_METHOD_SHA512 == hashMethod )
+                    || ( LdapSecurityConstants.HASH_METHOD_MD5 == hashMethod ) )
                 {
                     hashedPassword = LdifUtils.base64decodeToByteArray( rest );
                     salt = null;
                 }
-                else if ( HASH_METHOD_SSHA.equalsIgnoreCase( hashMethod )
-                    || HASH_METHOD_SSHA_256.equalsIgnoreCase( hashMethod )
-                    || HASH_METHOD_SSHA_384.equalsIgnoreCase( hashMethod )
-                    || HASH_METHOD_SSHA_512.equalsIgnoreCase( hashMethod )
-                    || HASH_METHOD_SMD5.equalsIgnoreCase( hashMethod ) )
+                else if ( ( LdapSecurityConstants.HASH_METHOD_SSHA == hashMethod )
+                    || ( LdapSecurityConstants.HASH_METHOD_SSHA256 == hashMethod )
+                    || ( LdapSecurityConstants.HASH_METHOD_SSHA384 == hashMethod )
+                    || ( LdapSecurityConstants.HASH_METHOD_SSHA512 == hashMethod )
+                    || ( LdapSecurityConstants.HASH_METHOD_SMD5 == hashMethod ) )
                 {
-                    if ( HASH_METHOD_SSHA.equalsIgnoreCase( hashMethod ) )
-                    {
-                        hashedPassword = new byte[20];
-                    }
-                    else if ( HASH_METHOD_SSHA_256.equalsIgnoreCase( hashMethod ) )
-                    {
-                        hashedPassword = new byte[32];
-                    }
-                    else if ( HASH_METHOD_SSHA_384.equalsIgnoreCase( hashMethod ) )
+                    switch ( hashMethod )
                     {
-                        hashedPassword = new byte[48];
-                    }
-                    else if ( HASH_METHOD_SSHA_512.equalsIgnoreCase( hashMethod ) )
-                    {
-                        hashedPassword = new byte[64];
-                    }
-                    else if ( HASH_METHOD_SMD5.equalsIgnoreCase( hashMethod ) )
-                    {
-                        hashedPassword = new byte[16];
+                        case HASH_METHOD_SSHA:
+                            hashedPassword = new byte[20];
+                            break;
+                        case HASH_METHOD_SSHA256:
+                            hashedPassword = new byte[32];
+                            break;
+                        case HASH_METHOD_SSHA384:
+                            hashedPassword = new byte[48];
+                            break;
+                        case HASH_METHOD_SSHA512:
+                            hashedPassword = new byte[64];
+                            break;
+                        case HASH_METHOD_SMD5:
+                            hashedPassword = new byte[16];
+                            break;
+                        default:
+                            break;
                     }
 
                     byte[] hashedPasswordWithSalt = LdifUtils.base64decodeToByteArray( rest
);
                     salt = new byte[hashedPasswordWithSalt.length - hashedPassword.length];
                     split( hashedPasswordWithSalt, hashedPassword, salt );
                 }
-                else if ( HASH_METHOD_CRYPT.equalsIgnoreCase( hashMethod ) )
+                else if ( LdapSecurityConstants.HASH_METHOD_CRYPT == hashMethod )
                 {
                     byte[] saltWithPassword = LdifUtils.utf8encode( rest );
                     salt = new byte[2];
@@ -182,14 +155,14 @@ public class Password
                 }
                 else
                 {
-                    hashMethod = HASH_METHOD_UNSUPPORTED;
+                    isUnsupportedHashMethod = true;
                     trash = password;
                 }
             }
             catch ( RuntimeException e )
             {
                 // happens if 'rest' is not valid BASE64
-                hashMethod = HASH_METHOD_INVALID;
+                isInvalidHashValue = true;
                 trash = password;
             }
         }
@@ -209,44 +182,30 @@ public class Password
      * @param hashMethod the hash method to use
      * @param passwordAsPlaintext the plain text password
      * 
-     * @throws IllegalArgumentException if the given hash method is not
-     *         supported of if the given password is null
+     * @throws IllegalArgumentException if the given password is null
      */
-    public Password( String hashMethod, String passwordAsPlaintext )
+    public Password( LdapSecurityConstants hashMethod, String passwordAsPlaintext )
     {
-        if ( !( hashMethod == null || HASH_METHOD_NO.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SHA.equalsIgnoreCase( hashMethod ) || HASH_METHOD_SSHA.equalsIgnoreCase(
hashMethod )
-            || HASH_METHOD_SHA_256.equalsIgnoreCase( hashMethod ) || HASH_METHOD_SSHA_256.equalsIgnoreCase(
hashMethod )
-            || HASH_METHOD_SHA_384.equalsIgnoreCase( hashMethod ) || HASH_METHOD_SSHA_384.equalsIgnoreCase(
hashMethod )
-            || HASH_METHOD_SHA_512.equalsIgnoreCase( hashMethod ) || HASH_METHOD_SSHA_512.equalsIgnoreCase(
hashMethod )
-            || HASH_METHOD_MD5.equalsIgnoreCase( hashMethod ) || HASH_METHOD_SMD5.equalsIgnoreCase(
hashMethod ) || HASH_METHOD_CRYPT
-                .equalsIgnoreCase( hashMethod ) ) )
-        {
-            throw new IllegalArgumentException( BrowserCoreMessages.model__unsupported_hash
);
-        }
+        // Checking the password as plain text
         if ( passwordAsPlaintext == null )
         {
             throw new IllegalArgumentException( BrowserCoreMessages.model__empty_password
);
         }
 
-        // set hash method
-        if ( HASH_METHOD_NO.equalsIgnoreCase( hashMethod ) )
-        {
-            hashMethod = null;
-        }
+        // Setting the hash method
         this.hashMethod = hashMethod;
 
-        // set salt
-        if ( HASH_METHOD_SSHA.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_256.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_384.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_512.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SMD5.equalsIgnoreCase( hashMethod ) )
+        // Setting the salt
+        if ( ( LdapSecurityConstants.HASH_METHOD_SSHA == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA256 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA384 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA512 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SMD5 == hashMethod ) )
         {
             this.salt = new byte[8];
             new SecureRandom().nextBytes( this.salt );
         }
-        else if ( HASH_METHOD_CRYPT.equalsIgnoreCase( hashMethod ) )
+        else if ( LdapSecurityConstants.HASH_METHOD_CRYPT == hashMethod )
         {
             this.salt = new byte[2];
             SecureRandom sr = new SecureRandom();
@@ -260,40 +219,28 @@ public class Password
             this.salt = null;
         }
 
-        // digest
-        if ( HASH_METHOD_SHA.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA.equalsIgnoreCase( hashMethod ) )
-        {
-            this.hashedPassword = digest( HASH_METHOD_SHA, passwordAsPlaintext, this.salt
);
-        }
-        else if ( HASH_METHOD_SHA_256.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_256.equalsIgnoreCase( hashMethod ) )
-        {
-            this.hashedPassword = digest( HASH_METHOD_SHA_256, passwordAsPlaintext, this.salt
);
-        }
-        else if ( HASH_METHOD_SHA_384.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_384.equalsIgnoreCase( hashMethod ) )
-        {
-            this.hashedPassword = digest( HASH_METHOD_SHA_384, passwordAsPlaintext, this.salt
);
-        }
-        else if ( HASH_METHOD_SHA_512.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_512.equalsIgnoreCase( hashMethod ) )
+        // Setting the hashed password
+        if ( hashMethod == null )
         {
-            this.hashedPassword = digest( HASH_METHOD_SHA_512, passwordAsPlaintext, this.salt
);
+            this.hashedPassword = LdifUtils.utf8encode( passwordAsPlaintext );
         }
-        else if ( HASH_METHOD_MD5.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SMD5.equalsIgnoreCase( hashMethod ) )
+        else if ( ( LdapSecurityConstants.HASH_METHOD_SHA == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SHA256 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA256 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SHA384 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA384 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SHA512 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA512 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_MD5 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SMD5 == hashMethod ) )
         {
-            this.hashedPassword = digest( HASH_METHOD_MD5, passwordAsPlaintext, this.salt
);
+            this.hashedPassword = digest( hashMethod, passwordAsPlaintext, this.salt );
         }
-        else if ( HASH_METHOD_CRYPT.equalsIgnoreCase( hashMethod ) )
+        else if ( LdapSecurityConstants.HASH_METHOD_CRYPT == hashMethod )
         {
             this.hashedPassword = crypt( passwordAsPlaintext, this.salt );
         }
-        else if ( hashMethod == null )
-        {
-            this.hashedPassword = LdifUtils.utf8encode( passwordAsPlaintext );
-        }
     }
 
 
@@ -312,41 +259,26 @@ public class Password
         }
 
         boolean verified = false;
+
         if ( hashMethod == null )
         {
             verified = testPasswordAsPlaintext.equals( LdifUtils.utf8decode( hashedPassword
) );
         }
-        else if ( HASH_METHOD_SHA.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA.equalsIgnoreCase( hashMethod ) )
+        else if ( ( LdapSecurityConstants.HASH_METHOD_SHA == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SHA256 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA256 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SHA384 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA384 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SHA512 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SSHA512 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_MD5 == hashMethod )
+            || ( LdapSecurityConstants.HASH_METHOD_SMD5 == hashMethod ) )
         {
-            byte[] hash = digest( HASH_METHOD_SHA, testPasswordAsPlaintext, salt );
+            byte[] hash = digest( hashMethod, testPasswordAsPlaintext, salt );
             verified = equals( hash, hashedPassword );
         }
-        else if ( HASH_METHOD_SHA_256.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_256.equalsIgnoreCase( hashMethod ) )
-        {
-            byte[] hash = digest( HASH_METHOD_SHA_256, testPasswordAsPlaintext, salt );
-            verified = equals( hash, hashedPassword );
-        }
-        else if ( HASH_METHOD_SHA_384.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_384.equalsIgnoreCase( hashMethod ) )
-        {
-            byte[] hash = digest( HASH_METHOD_SHA_384, testPasswordAsPlaintext, salt );
-            verified = equals( hash, hashedPassword );
-        }
-        else if ( HASH_METHOD_SHA_512.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SSHA_512.equalsIgnoreCase( hashMethod ) )
-        {
-            byte[] hash = digest( HASH_METHOD_SHA_512, testPasswordAsPlaintext, salt );
-            verified = equals( hash, hashedPassword );
-        }
-        else if ( HASH_METHOD_MD5.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_SMD5.equalsIgnoreCase( hashMethod ) )
-        {
-            byte[] hash = digest( HASH_METHOD_MD5, testPasswordAsPlaintext, salt );
-            verified = equals( hash, hashedPassword );
-        }
-        else if ( HASH_METHOD_CRYPT.equalsIgnoreCase( hashMethod ) )
+        else if ( LdapSecurityConstants.HASH_METHOD_CRYPT == hashMethod )
         {
             byte[] crypted = crypt( testPasswordAsPlaintext, salt );
             verified = equals( crypted, hashedPassword );
@@ -361,7 +293,7 @@ public class Password
      * 
      * @return the hash method
      */
-    public String getHashMethod()
+    public LdapSecurityConstants getHashMethod()
     {
         return hashMethod;
     }
@@ -429,20 +361,19 @@ public class Password
     {
         StringBuffer sb = new StringBuffer();
 
-        if ( HASH_METHOD_UNSUPPORTED.equalsIgnoreCase( hashMethod )
-            || HASH_METHOD_INVALID.equalsIgnoreCase( hashMethod ) )
+        if ( isUnsupportedHashMethod || isInvalidHashValue )
         {
             sb.append( trash );
         }
-        else if ( HASH_METHOD_CRYPT.equalsIgnoreCase( hashMethod ) )
+        else if ( LdapSecurityConstants.HASH_METHOD_CRYPT == hashMethod )
         {
-            sb.append( '{' ).append( hashMethod ).append( '}' );
+            sb.append( '{' ).append( hashMethod.getPrefix() ).append( '}' );
             sb.append( LdifUtils.utf8decode( salt ) );
             sb.append( LdifUtils.utf8decode( hashedPassword ) );
         }
         else if ( hashMethod != null )
         {
-            sb.append( '{' ).append( hashMethod ).append( '}' );
+            sb.append( '{' ).append( hashMethod.getPrefix() ).append( '}' );
             if ( salt != null )
             {
                 byte[] hashedPasswordWithSaltBytes = new byte[hashedPassword.length + salt.length];
@@ -477,20 +408,30 @@ public class Password
     }
 
 
+    /**
+     * Checks equality between two byte arrays.
+     *
+     * @param data1 the first byte array
+     * @param data2 the first byte array
+     * @return <code>true</code> if the two byte arrays are equal
+     */
     private static boolean equals( byte[] data1, byte[] data2 )
     {
         if ( data1 == data2 )
         {
             return true;
         }
+
         if ( data1 == null || data2 == null )
         {
             return false;
         }
+
         if ( data1.length != data2.length )
         {
             return false;
         }
+
         for ( int i = 0; i < data1.length; i++ )
         {
             if ( data1[i] != data2[i] )
@@ -498,44 +439,59 @@ public class Password
                 return false;
             }
         }
+
         return true;
     }
 
 
-    private static byte[] digest( String hashMethod, String password, byte[] salt )
+    /**
+     * Computes the hashed value of a password with the given hash method and optional salt.
+     *
+     * @param hashMethod the hash method
+     * @param password the password
+     * @param salt the optional salt (can be <code>null</code>)
+     * @return the hashed value of the password
+     */
+    private static byte[] digest( LdapSecurityConstants hashMethod, String password, byte[]
salt )
     {
-
+        // Converting password to byte array
         byte[] passwordBytes = LdifUtils.utf8encode( password );
-        MessageDigest digest;
+
+        // Getting the message digest associated with the hash method
         try
         {
-            digest = MessageDigest.getInstance( hashMethod );
+            MessageDigest digest = MessageDigest.getInstance( hashMethod.getAlgorithm() );
+
+            // Computing the hashed password (salted or not)
+            if ( salt != null )
+            {
+                digest.update( passwordBytes );
+                digest.update( salt );
+                return digest.digest();
+            }
+            else
+            {
+                return digest.digest( passwordBytes );
+            }
         }
         catch ( NoSuchAlgorithmException e1 )
         {
             return null;
         }
-
-        if ( salt != null )
-        {
-            digest.update( passwordBytes );
-            digest.update( salt );
-            byte[] hashedPasswordBytes = digest.digest();
-            return hashedPasswordBytes;
-        }
-        else
-        {
-            byte[] hashedPasswordBytes = digest.digest( passwordBytes );
-            return hashedPasswordBytes;
-        }
     }
 
 
+    /**
+     * Computes the crypt value of a password (with salt).
+     *
+     * @param password the password
+     * @param salt the salt
+     * @return the crypt value of the password
+     */
     private static byte[] crypt( String password, byte[] salt )
     {
         String saltWithCrypted = UnixCrypt.crypt( password, LdifUtils.utf8decode( salt )
);
         String crypted = saltWithCrypted.substring( 2 );
         return LdifUtils.utf8encode( crypted );
     }
-
 }

Modified: directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordDialog.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordDialog.java?rev=1490334&r1=1490333&r2=1490334&view=diff
==============================================================================
--- directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordDialog.java
(original)
+++ directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordDialog.java
Thu Jun  6 16:12:27 2013
@@ -21,13 +21,13 @@
 package org.apache.directory.studio.valueeditors.password;
 
 
-import java.util.Arrays;
-
+import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants;
 import org.apache.directory.studio.common.ui.widgets.BaseWidgetUtils;
 import org.apache.directory.studio.connection.core.Connection;
 import org.apache.directory.studio.connection.core.ConnectionParameter.AuthenticationMethod;
 import org.apache.directory.studio.connection.core.jobs.CheckBindRunnable;
 import org.apache.directory.studio.connection.ui.RunnableContextRunner;
+import org.apache.directory.studio.ldapbrowser.core.BrowserCoreMessages;
 import org.apache.directory.studio.ldapbrowser.core.model.IEntry;
 import org.apache.directory.studio.ldapbrowser.core.model.Password;
 import org.apache.directory.studio.ldapbrowser.core.utils.Utils;
@@ -37,6 +37,12 @@ import org.eclipse.core.runtime.IStatus;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ArrayContentProvider;
+import org.eclipse.jface.viewers.ComboViewer;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.ModifyEvent;
 import org.eclipse.swt.events.ModifyListener;
@@ -45,7 +51,6 @@ import org.eclipse.swt.events.SelectionE
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
@@ -64,21 +69,24 @@ import org.eclipse.swt.widgets.Text;
  */
 public class PasswordDialog extends Dialog
 {
+    /** The constant for no hash method */
+    private static final String NO_HASH_METHOD = "NO-HASH-METHOD";
+
     /** The supported hash methods */
-    private static final String[] HASH_METHODS =
+    private static final Object[] HASH_METHODS =
         {
-            Password.HASH_METHOD_SHA,
-            Password.HASH_METHOD_SHA_256,
-            Password.HASH_METHOD_SHA_384,
-            Password.HASH_METHOD_SHA_512,
-            Password.HASH_METHOD_SSHA,
-            Password.HASH_METHOD_SSHA_256,
-            Password.HASH_METHOD_SSHA_384,
-            Password.HASH_METHOD_SSHA_512,
-            Password.HASH_METHOD_MD5,
-            Password.HASH_METHOD_SMD5,
-            Password.HASH_METHOD_CRYPT,
-            Password.HASH_METHOD_NO };
+            LdapSecurityConstants.HASH_METHOD_SHA,
+            LdapSecurityConstants.HASH_METHOD_SHA256,
+            LdapSecurityConstants.HASH_METHOD_SHA384,
+            LdapSecurityConstants.HASH_METHOD_SHA512,
+            LdapSecurityConstants.HASH_METHOD_SSHA,
+            LdapSecurityConstants.HASH_METHOD_SSHA256,
+            LdapSecurityConstants.HASH_METHOD_SSHA384,
+            LdapSecurityConstants.HASH_METHOD_SSHA512,
+            LdapSecurityConstants.HASH_METHOD_MD5,
+            LdapSecurityConstants.HASH_METHOD_SMD5,
+            LdapSecurityConstants.HASH_METHOD_CRYPT,
+            NO_HASH_METHOD };
 
     /** Constant for the Current Password tab */
     private static final int CURRENT_TAB = 0;
@@ -126,7 +134,7 @@ public class PasswordDialog extends Dial
     private TabItem newPasswordTab;
     private Composite newPasswordComposite;
     private Text newPasswordText;
-    private Combo newPasswordHashMethodCombo;
+    private ComboViewer newPasswordHashMethodComboViewer;
     private Text newPasswordPreviewText;
     private Text newPasswordPreviewValueHexText;
     private Text newPasswordPreviewSaltHexText;
@@ -187,9 +195,19 @@ public class PasswordDialog extends Dial
         }
 
         // save selected hash method to dialog settings, selected tab will be
-        // saved int close()
-        ValueEditorsActivator.getDefault().getDialogSettings().put( SELECTED_HASH_METHOD_DIALOGSETTINGS_KEY,
-            newPasswordHashMethodCombo.getText() );
+        // saved on close()
+        LdapSecurityConstants selectedHashMethod = getSelectedNewPasswordHashMethod();
+
+        if ( selectedHashMethod == null )
+        {
+            ValueEditorsActivator.getDefault().getDialogSettings().put( SELECTED_HASH_METHOD_DIALOGSETTINGS_KEY,
+                NO_HASH_METHOD );
+        }
+        else
+        {
+            ValueEditorsActivator.getDefault().getDialogSettings().put( SELECTED_HASH_METHOD_DIALOGSETTINGS_KEY,
+                selectedHashMethod.getName() );
+        }
 
         super.okPressed();
     }
@@ -230,13 +248,21 @@ public class PasswordDialog extends Dial
         catch ( Exception e )
         {
         }
+
         try
         {
-            String hashMethod = ValueEditorsActivator.getDefault().getDialogSettings().get(
+            String hashMethodName = ValueEditorsActivator.getDefault().getDialogSettings().get(
                 SELECTED_HASH_METHOD_DIALOGSETTINGS_KEY );
-            if ( Arrays.asList( HASH_METHODS ).contains( hashMethod ) )
+
+            LdapSecurityConstants hashMethod = LdapSecurityConstants.getAlgorithm( hashMethodName
);
+
+            if ( ( hashMethod == null ) || NO_HASH_METHOD.equals( hashMethodName ) )
+            {
+                newPasswordHashMethodComboViewer.setSelection( new StructuredSelection( NO_HASH_METHOD
) );
+            }
+            else
             {
-                newPasswordHashMethodCombo.setText( hashMethod );
+                newPasswordHashMethodComboViewer.setSelection( new StructuredSelection( hashMethod
) );
             }
         }
         catch ( Exception e )
@@ -338,7 +364,7 @@ public class PasswordDialog extends Dial
 
         // Show current password details button
         showCurrentPasswordDetailsButton = BaseWidgetUtils.createCheckbox( currentPasswordDetailsComposite,
Messages
-            .getString( "PasswordDialog.ShowCurrentPasswordDetails" ), 1 ); //$NON-NLS-1$
+            .getString( "PasswordDialog.ShowCurrentPasswordDetails" ), 2 ); //$NON-NLS-1$
 
         // Verify password text
         BaseWidgetUtils
@@ -400,7 +426,24 @@ public class PasswordDialog extends Dial
 
         // New password hashing method combo
         BaseWidgetUtils.createLabel( newPasswordComposite, Messages.getString( "PasswordDialog.SelectHashMethod"
), 1 ); //$NON-NLS-1$
-        newPasswordHashMethodCombo = BaseWidgetUtils.createReadonlyCombo( newPasswordComposite,
HASH_METHODS, 0, 1 );
+        newPasswordHashMethodComboViewer = new ComboViewer( newPasswordComposite );
+        newPasswordHashMethodComboViewer.setContentProvider( new ArrayContentProvider() );
+        newPasswordHashMethodComboViewer.setLabelProvider( new LabelProvider()
+        {
+            public String getText( Object element )
+            {
+                String hashMethod = getHashMethodName( element );
+
+                if ( !"".equals( hashMethod ) )
+                {
+                    return hashMethod;
+                }
+
+                return super.getText( element );
+            }
+        } );
+        newPasswordHashMethodComboViewer.setInput( HASH_METHODS );
+        newPasswordHashMethodComboViewer.getControl().setLayoutData( new GridData( SWT.FILL,
SWT.CENTER, true, false ) );
 
         // New password preview text
         BaseWidgetUtils.createLabel( newPasswordComposite, Messages.getString( "PasswordDialog.PasswordPreview"
), 1 ); //$NON-NLS-1$
@@ -428,7 +471,7 @@ public class PasswordDialog extends Dial
 
         // Show new password details button
         showNewPasswordDetailsButton = BaseWidgetUtils.createCheckbox( newPasswordPreviewDetailsComposite,
Messages
-            .getString( "PasswordDialog.ShowNewPasswordDetails" ), 1 ); //$NON-NLS-1$
+            .getString( "PasswordDialog.ShowNewPasswordDetails" ), 2 ); //$NON-NLS-1$
 
         // New password tab
         newPasswordTab = new TabItem( tabFolder, SWT.NONE );
@@ -451,7 +494,7 @@ public class PasswordDialog extends Dial
                     updateCurrentPasswordGroup();
                 }
             } );
-            
+
             testPasswordText.addModifyListener( new ModifyListener()
             {
                 public void modifyText( ModifyEvent e )
@@ -459,7 +502,7 @@ public class PasswordDialog extends Dial
                     updateCurrentPasswordGroup();
                 }
             } );
-            
+
             showTestPasswordDetailsButton.addSelectionListener( new SelectionAdapter()
             {
                 public void widgetSelected( SelectionEvent arg0 )
@@ -493,9 +536,9 @@ public class PasswordDialog extends Dial
             }
         } );
 
-        newPasswordHashMethodCombo.addSelectionListener( new SelectionAdapter()
+        newPasswordHashMethodComboViewer.addSelectionChangedListener( new ISelectionChangedListener()
         {
-            public void widgetSelected( SelectionEvent event )
+            public void selectionChanged( SelectionChangedEvent event )
             {
                 updateNewPasswordGroup();
             }
@@ -527,7 +570,7 @@ public class PasswordDialog extends Dial
         // set current password to the UI widgets
         if ( currentPassword != null )
         {
-            currentPasswordHashMethodText.setText( Utils.getNonNullString( currentPassword.getHashMethod()
) );
+            currentPasswordHashMethodText.setText( getCurrentPasswordHashMethodName() );
             currentPasswordValueHexText.setText( Utils
                 .getNonNullString( currentPassword.getHashedPasswordAsHexString() ) );
             currentPasswordSaltHexText.setText( Utils.getNonNullString( currentPassword.getSaltAsHexString()
) );
@@ -641,7 +684,7 @@ public class PasswordDialog extends Dial
     private void updateNewPasswordGroup()
     {
         // set new password to the UI widgets
-        newPassword = new Password( newPasswordHashMethodCombo.getText(), newPasswordText.getText()
);
+        newPassword = new Password( getSelectedNewPasswordHashMethod(), newPasswordText.getText()
);
         if ( !"".equals( newPasswordText.getText() ) || newPassword.getHashMethod() == null
) //$NON-NLS-1$
         {
             newPasswordPreviewValueHexText
@@ -705,6 +748,72 @@ public class PasswordDialog extends Dial
 
 
     /**
+     * Gets the selected new password hash method.
+     *
+     * @return the selected new password hash method
+     */
+    private LdapSecurityConstants getSelectedNewPasswordHashMethod()
+    {
+        StructuredSelection selection = ( StructuredSelection ) newPasswordHashMethodComboViewer.getSelection();
+
+        if ( !selection.isEmpty() )
+        {
+            Object selectedObject = selection.getFirstElement();
+
+            if ( selectedObject instanceof LdapSecurityConstants )
+            {
+                return ( LdapSecurityConstants ) selectedObject;
+            }
+        }
+
+        return null;
+    }
+
+
+    /**
+     * Gets the name of the hash method.
+     *
+     * @param o the hash method object
+     * @return the name of the hash method
+     */
+    private String getHashMethodName( Object o )
+    {
+        if ( o instanceof LdapSecurityConstants )
+        {
+            LdapSecurityConstants hashMethod = ( LdapSecurityConstants ) o;
+
+            return hashMethod.getName();
+        }
+        else if ( ( o instanceof String ) && NO_HASH_METHOD.equals( o ) )
+        {
+            return BrowserCoreMessages.model__no_hash;
+        }
+
+        return null;
+    }
+
+
+    /**
+     * Gets the current password hash method name.
+     *
+     * @return the current password hash method name
+     */
+    private String getCurrentPasswordHashMethodName()
+    {
+        LdapSecurityConstants hashMethod = currentPassword.getHashMethod();
+
+        if ( hashMethod != null )
+        {
+            return Utils.getNonNullString( getHashMethodName( hashMethod ) );
+        }
+        else
+        {
+            return Utils.getNonNullString( getHashMethodName( NO_HASH_METHOD ) );
+        }
+    }
+
+
+    /**
      * Gets the new password.
      * 
      * @return the password, either encypted by the selected

Modified: directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordValueEditor.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordValueEditor.java?rev=1490334&r1=1490333&r2=1490334&view=diff
==============================================================================
--- directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordValueEditor.java
(original)
+++ directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/PasswordValueEditor.java
Thu Jun  6 16:12:27 2013
@@ -21,11 +21,13 @@
 package org.apache.directory.studio.valueeditors.password;
 
 
+import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants;
 import org.apache.directory.studio.ldapbrowser.common.dialogs.TextDialog;
 import org.apache.directory.studio.ldapbrowser.core.model.IAttribute;
 import org.apache.directory.studio.ldapbrowser.core.model.IEntry;
 import org.apache.directory.studio.ldapbrowser.core.model.IValue;
 import org.apache.directory.studio.valueeditors.AbstractDialogBinaryValueEditor;
+import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.widgets.Shell;
 
 
@@ -99,7 +101,8 @@ public class PasswordValueEditor extends
                 else if ( password.indexOf( '{' ) == 0 && password.indexOf( '}' )
> 0 )
                 {
                     String hashMethod = password.substring( password.indexOf( '{' ) + 1,
password.indexOf( '}' ) );
-                    text = hashMethod + Messages.getString( "PasswordValueEditor.HashedPassword"
); //$NON-NLS-1$
+                    text = NLS.bind(
+                        Messages.getString( "PasswordValueEditor.HashedPassword" ), getHashMethodName(
hashMethod ) ); //$NON-NLS-1$
                 }
                 else
                 {
@@ -112,6 +115,25 @@ public class PasswordValueEditor extends
 
 
     /**
+     * Gets the name of the hash method.
+     *
+     * @param s the hash method string
+     * @return the name of the associated hash method or the given string
+     */
+    private String getHashMethodName( String s )
+    {
+        LdapSecurityConstants hashMethod = LdapSecurityConstants.getAlgorithm( s );
+
+        if ( hashMethod != null )
+        {
+            return hashMethod.getName();
+        }
+
+        return s;
+    }
+
+
+    /**
      * {@inheritDoc}
      * 
      * Returns a PasswordValueEditorRawValueWrapper with empty 

Modified: directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages.properties
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages.properties?rev=1490334&r1=1490333&r2=1490334&view=diff
==============================================================================
--- directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages.properties
(original)
+++ directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages.properties
Thu Jun  6 16:12:27 2013
@@ -38,5 +38,5 @@ PasswordDialog.ShowTestPasswordDetails=S
 PasswordDialog.Verify=Verify
 PasswordDialog.VerifyPassword=Verify Password:
 PasswordValueEditor.EmptyPassword=Empty password
-PasswordValueEditor.HashedPassword=\ hashed password
+PasswordValueEditor.HashedPassword={0} hashed password
 PasswordValueEditor.PlainTextPassword=Plain text password

Modified: directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_de.properties
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_de.properties?rev=1490334&r1=1490333&r2=1490334&view=diff
==============================================================================
--- directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_de.properties
(original)
+++ directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_de.properties
Thu Jun  6 16:12:27 2013
@@ -37,5 +37,5 @@ PasswordDialog.ShowTestPasswordDetails=Z
 PasswordDialog.Verify=Verifiziere
 PasswordDialog.VerifyPassword=Verifiziere Passwort\:
 PasswordValueEditor.EmptyPassword=Leeres Passwort
-PasswordValueEditor.HashedPassword=\ gehashtes Passwort
+PasswordValueEditor.HashedPassword={0} gehashtes Passwort
 PasswordValueEditor.PlainTextPassword=Klartext Passwort

Modified: directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_fr.properties
URL: http://svn.apache.org/viewvc/directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_fr.properties?rev=1490334&r1=1490333&r2=1490334&view=diff
==============================================================================
--- directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_fr.properties
(original)
+++ directory/studio/trunk/plugins/valueeditors/src/main/java/org/apache/directory/studio/valueeditors/password/messages_fr.properties
Thu Jun  6 16:12:27 2013
@@ -38,5 +38,5 @@ PasswordDialog.ShowTestPasswordDetails=A
 PasswordDialog.Verify=V\u00E9rifier
 PasswordDialog.VerifyPassword=V\u00E9rifier le mot de passe:
 PasswordValueEditor.EmptyPassword=Mot de passe vide
-PasswordValueEditor.HashedPassword=\ mot de passe hach\u00E9
+PasswordValueEditor.HashedPassword=Mot de passe hach\u00E9 {0}
 PasswordValueEditor.PlainTextPassword=Mot de passe sous forme de texte brut



Mime
View raw message