Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Classes_Password
- {
- /**
- * Reference to database connection
- *
- * @var object Zend_DB
- */
- protected $_db = null;
- /**
- * Hashing algorithm to use
- *
- * @var string
- */
- protected $_hashAlgorithm = 'tiger192,4';
- private $_passwordHash = null;
- public function __construct()
- {
- // Assign reference to database connection
- $this->_db = Zend_Registry::get('db');
- }
- /**
- * Create password hash
- *
- * - Create a salt by hashing time() as random string
- * - Take the first 24 characters of the salt for use later
- * - Create encryption of the password by hashing the concatenation of the first 24 characters of the salt and the password
- * - Take the first 24 characters of the encryption for use later
- * - Hash to store in the database is the concatenation of the first 24 characters of the salt and the first 24 characters of the encryption
- *
- * @param string Password
- * @return string Encryption string
- */
- public function encrypt( $password )
- {
- // Create a salt by hashing time() as random string
- $salt = hash( $this->_hashAlgorithm, time() );
- // Take the first 24 characters of the salt for use later
- $halfSalt = substr( $salt, 0, 24 );
- // Create encryption of the password by hashing the concatenation of the first 24 characters of the salt and the password
- $encryption = hash( $this->_hashAlgorithm, $halfSalt.$password );
- // Take the first 24 characters of the encryption for use later
- $halfEncryption = substr( $encryption, 0, 24 );
- return $halfSalt.$halfEncryption;
- }
- /**
- * Determine whether provided password is valid or not
- *
- * @param string Password
- * @return boolean true|false
- */
- public function isValid( $encryptionString, $password )
- {
- $halfSalt = substr( $encryptionString, 0, 24 );
- $halfEncryption = substr( $encryptionString, 24 );
- return ( $halfEncryption == substr( hash( $this->_hashAlgorithm, $halfSalt.$password ), 0, 24 ) ) ? true : false;
- }
- public function getEncryptionSalt( $username )
- {
- if ( ! isset( $this->_passwordHash ) )
- {
- $this->_passwordHash = substr( $this->_getPasswordHash($username), 0, 24 );
- }
- return $this->_passwordHash;
- }
- public function getEncryptionString( $username, $password )
- {
- $encryptionString = $this->getEncryptionSalt($username) . substr( hash( $this->_hashAlgorithm, $this->getEncryptionSalt($username).$password ), 0, 24 );
- return $encryptionString;
- }
- protected function _getPasswordHash( $username )
- {
- $result = $this->_db->fetchRow("
- SELECT
- password
- FROM
- credentials
- WHERE
- username = '" . $username . "'
- ");
- return $result->password;
- }
- }
Add Comment
Please, Sign In to add comment