Login   Register  
PHP Classes
elePHPant
Icontem

File: Passwd.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Eric Renfro  >  Passwd  >  Passwd.inc  >  Download  
File: Passwd.inc
Role: ???
Content type: text/plain
Description: UNIX Crypt MD5 hashing algorithm
Class: Passwd
Author: By
Last change:
Date: 2002-03-09 01:23
Size: 2,133 bytes
 

Contents

Class file image Download
<?

/* 
   --------------------------------------------------------------------------
   PHP::Passwd                                                 by Eric Renfro
   ------------------------------------------------- psi-jack@myrddincd.com -
   This is a UNIX-style crypt-md5 PHP Class object, for hashing out, and
   checking salted passwords for authentication purposes. It was made for
   PHP 4, and was intended to be used with a CRYPT_MD5-capable crypt() lib.
   --------------------------------------------------------------------------
   To use this:

   $variable = new Passwd;

   Encrytping a cleartext password:
     $encrypted = $variable->encrypt("Password");

   Checking a password to a salted hash:
     if ($variable->check("Password", "<encrypted hash>")) {
       ...
     } else {
       ...
     }

   Generating a random 8-character password:
     $random = $variable->random_pass;
   --------------------------------------------------------------------------
   Hope you enjoy!
   --------------------------------------------------------------------------
*/


class Passwd {

  function encrypt($password, $salt = '') {
    $MAGIC = '$1$';
    $ITOA64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

    if (strlen($salt) < 8) {
      mt_srand ((double) microtime() * 1000000);

      for ($i = 8; $i > strlen($salt);) {
        $salt .= $ITOA64{mt_rand (0, strlen($ITOA64))};
      }
    }

    $passwd = crypt($password, $MAGIC . $salt);

    return($passwd);
  }

  function salt_of($hash) {
    return(substr($hash, 3, 8));
  }

  function check($password, $hash) {
    $test = $this->encrypt($password, $this->salt_of($hash));

    if ($test == $hash) {
      return(true);
    } else {
      return(false);
    }
  }

  function random_pass() {
    $ITOA64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

    mt_srand ((double) microtime() * 1000000);

    for ($i = 8; $i > strlen($salt);) {
      $salt .= $ITOA64{mt_rand (0, strlen($ITOA64))};
    }

    return($salt);
  }
}