[seam-commits] Seam SVN: r7803 - in trunk/src: test/unit/org/jboss/seam/test/unit and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Apr 3 03:20:00 EDT 2008


Author: shane.bryzak at jboss.com
Date: 2008-04-03 03:20:00 -0400 (Thu, 03 Apr 2008)
New Revision: 7803

Modified:
   trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
   trunk/src/main/org/jboss/seam/security/management/PasswordHash.java
   trunk/src/test/unit/org/jboss/seam/test/unit/PasswordHashTest.java
Log:
made PasswordHash a Seam component

Modified: trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java	2008-04-03 01:03:11 UTC (rev 7802)
+++ trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java	2008-04-03 07:20:00 UTC (rev 7803)
@@ -191,7 +191,7 @@
          }
          else
          {
-            account.setPasswordHash(PasswordHash.generateHash(password, getAccountSalt(account)));
+            account.setPasswordHash(PasswordHash.instance().generateSaltedHash(password, getAccountSalt(account)));
             account.setEnabled(true);            
          }
          
@@ -379,7 +379,7 @@
          throw new NoSuchUserException("Could not change password, user '" + name + "' does not exist");
       }
       
-      account.setPasswordHash(PasswordHash.generateHash(password, getAccountSalt(account)));
+      account.setPasswordHash(PasswordHash.instance().generateSaltedHash(password, getAccountSalt(account)));
       mergeAccount(account);
       return true;
    }
@@ -463,7 +463,7 @@
          return false;
       }
       
-      String passwordHash = PasswordHash.generateHash(password, getAccountSalt(account));
+      String passwordHash = PasswordHash.instance().generateSaltedHash(password, getAccountSalt(account));
       boolean success = passwordHash.equals(account.getPasswordHash());
             
       if (success && Events.exists())

Modified: trunk/src/main/org/jboss/seam/security/management/PasswordHash.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/PasswordHash.java	2008-04-03 01:03:11 UTC (rev 7802)
+++ trunk/src/main/org/jboss/seam/security/management/PasswordHash.java	2008-04-03 07:20:00 UTC (rev 7803)
@@ -1,7 +1,16 @@
 package org.jboss.seam.security.management;
 
+import static org.jboss.seam.ScopeType.STATELESS;
+import static org.jboss.seam.annotations.Install.BUILT_IN;
+
 import java.security.MessageDigest;
 
+import org.jboss.seam.Component;
+import org.jboss.seam.ScopeType;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
 import org.jboss.seam.util.Base64;
 
 /**
@@ -9,31 +18,36 @@
  *  
  * @author Shane Bryzak
  */
+ at Scope(STATELESS)
+ at Name("org.jboss.seam.security.management.passwordHash")
+ at Install(precedence = BUILT_IN)
+ at BypassInterceptors
 public class PasswordHash
 {
-   public enum Algorithm {SHA, MD5}
+   public static final String ALGORITHM_MD5 = "MD5";
+   public static final String ALGORITHM_SHA = "SHA";
+      
+   private static final String DEFAULT_ALGORITHM = ALGORITHM_MD5;
    
-   private static final Algorithm DEFAULT_ALGORITHM = Algorithm.MD5;
-   
-   public static String generateHash(String password)
+   public String generateHash(String password)
    {
       return generateHash(password, DEFAULT_ALGORITHM);
    }
    
-   public static String generateHash(String password, Algorithm algorithm)
+   public String generateHash(String password, String algorithm)
    {
-      return generateHash(password, algorithm, null);
+      return generateSaltedHash(password, null, algorithm);
    }
    
-   public static String generateHash(String password, String saltPhrase)
+   public String generateSaltedHash(String password, String saltPhrase)
    {
-      return generateHash(password, DEFAULT_ALGORITHM, saltPhrase);
+      return generateSaltedHash(password, saltPhrase, DEFAULT_ALGORITHM);
    }
    
-   public static String generateHash(String password, Algorithm algorithm, String saltPhrase)
+   public String generateSaltedHash(String password, String saltPhrase, String algorithm)
    {
       try {        
-         MessageDigest md = MessageDigest.getInstance(algorithm.name());
+         MessageDigest md = MessageDigest.getInstance(algorithm);
                   
          if (saltPhrase != null)
          {
@@ -56,4 +70,9 @@
          throw new RuntimeException(e);        
      } 
    }
+   
+   public static PasswordHash instance()
+   {
+      return (PasswordHash) Component.getInstance(PasswordHash.class, ScopeType.STATELESS);
+   }
 }

Modified: trunk/src/test/unit/org/jboss/seam/test/unit/PasswordHashTest.java
===================================================================
--- trunk/src/test/unit/org/jboss/seam/test/unit/PasswordHashTest.java	2008-04-03 01:03:11 UTC (rev 7802)
+++ trunk/src/test/unit/org/jboss/seam/test/unit/PasswordHashTest.java	2008-04-03 07:20:00 UTC (rev 7803)
@@ -1,7 +1,6 @@
 package org.jboss.seam.test.unit;
 
 import org.jboss.seam.security.management.PasswordHash;
-import org.jboss.seam.security.management.PasswordHash.Algorithm;
 import org.testng.annotations.Test;
 
 public class PasswordHashTest
@@ -9,14 +8,14 @@
    @Test
    public void testMd5Hash()
    {
-      String hash = PasswordHash.generateHash("secret", Algorithm.MD5);
+      String hash = PasswordHash.instance().generateHash("secret", "MD5");
       assert hash.equals("Xr4ilOzQ4PCOq3aQ0qbuaQ==");
    }
    
    @Test
    public void testShaHash()
    {
-      String hash = PasswordHash.generateHash("secret", Algorithm.SHA);
+      String hash = PasswordHash.instance().generateHash("secret", "SHA");
       assert hash.equals("5en6G6MezRroT3XKqkdPOmY/BfQ=");
    }
 }




More information about the seam-commits mailing list