Improved password storage schema
Improved password storage schema

--- a/lib/auth.class.php
+++ b/lib/auth.class.php
@@ -128,7 +128,7 @@
 	$user->salt = $this->createSalt();
 
 	// Salt the password - why the hell was I using MD5 here?? I don't remember doing that - embarassing
-	$user->pass = $this->blowfishCrypt(md5($pass.$user->salt),12);
+	$user->pass = $this->blowfishCrypt($pass.$user->salt,12);
 
 	// Get the plaintext password out of memory
 	unset($pass);
@@ -163,7 +163,7 @@
 $user->salt = $this->createSalt();
 
 // Salt the password
-$user->pass = $this->blowfishCrypt(md5($plaintextPass.$user->salt),12);
+$user->pass = $this->blowfishCrypt($plaintextPass.$user->salt,12);
 $user->RealName = $RealName;
 $user->groups = $groups;
 $user->username = $username;
@@ -251,12 +251,20 @@
 // Get the valid hash out of memory as we have it in an array anyway
 unset($user->pass);
 
-    if( crypt(md5($password.$pass[1]),$pass[0]) != $pass[0]){
-      return $this->logFailedAttempt($username,$db);
+
+    if (crypt($password.$pass[1],$pass[0]) != $pass[0]){
+	// Check for a match on the old schema, there's only a risk of hash collision if the stored password is MD5
+	if (md5($password.$pass[1]) == $pass[0]){
+	  // We need to update the stored hash to use the new schema. Still don't remember going with MD5. Should know better!
+	  $db->updPass($username,$this->blowfishCrypt($password.$pass[1],12),$pass[1]);
+	}else{
+	  return $this->logFailedAttempt($username,$db);
+	}
+
       }
 
 // Create a Session ID
-    $sessID = md5(date('YmdHis') . mt_rand(10,80000) . mt_rand(11,500) . $username . mt_rand(0,90000));
+    $sessID = sha1(date('YmdHis') . mt_rand(10,80000) . mt_rand(11,500) . $username . mt_rand(0,90000));
 
 // Get the hashes out of memory
     unset($password);
@@ -339,6 +347,7 @@
 }
 
 
+
 /** Invalid Login
 *
 */

--- a/lib/customer.class.php
+++ b/lib/customer.class.php
@@ -33,7 +33,8 @@
 // We add the customer to the portal, even if we won't let them log-in (i.e. the portal is disabled)
 $password = $auth->generatePassword();
 $salt = $auth->createSalt();
-$pass = md5($password.$salt);
+$pass = ProgAuth::blowfishCrypt($password.$salt,12);
+
 
 
 if ($db->addCusttoPortal($id,$email,$pass.":".$salt,1)) {
@@ -77,7 +78,7 @@
 // We add the customer to the portal, even if we won't let them log-in (i.e. the portal is disabled)
 $password = $auth->generatePassword();
 $salt = $auth->createSalt();
-$pass = md5($password.$salt);
+$pass = ProgAuth::blowfishCrypt($password.$salt,12);
 
 
  global $notifications;

--- a/lib/db/authdb.class.php
+++ b/lib/db/authdb.class.php
@@ -213,6 +213,25 @@
 
 return $this->runQuery();
 }
+
+
+
+/** Update a user's password hash in the database
+*
+*/
+function updPass($user,$hash,$salt){
+  $crypt = new Crypto;
+  $user = $this->stringEscape($user);
+  $hash = $this->stringEscape($crypt->encrypt($hash.":".$salt,'auth'));
+
+  $sql = "UPDATE #__Users SET `pass`='$hash' WHERE `username`='$user'";
+  
+  $this->setQuery($sql);
+  $result = $this->runQuery();
+
+}
+
+
 
 
 /** Edit user