<?php
/**
* Generate passwords based on a hash algoritm.
*
* @author Stijn Leenknegt <stijnleenknegt@gmail.com>
* @version 1.0
*/
class Password
{
/**
* The list of hash algoritms.
*/
const HASH_ALGORITM_MD4 = 'md4';
const HASH_ALGORITM_MD5 = 'md5';
const HASH_ALGORITM_SHA1 = 'sha1';
const HASH_ALGORITM_SHA256 = 'sha256';
const HASH_ALGORITM_SHA384 = 'sha384';
const HASH_ALGORITM_SHA512 = 'sha512';
const HASH_ALGORITM_RIPEMD128 = 'ripemd128';
const HASH_ALOGIRTM_RIPEMD160 = 'ripemd160';
const HASH_ALGORITM_WHIRLPOOL = 'whirlpool';
const HASH_ALGORITM_TIGER128_3 = 'tiger128,3';
const HASH_ALGORITM_TIGER160_3 = 'tiger160,3';
const HASH_ALGORITM_TIGER192_3 = 'tiger192,3';
const HASH_ALGORITM_SNEFRU = 'snefru';
const HASH_ALGORITM_GOST = 'gost';
const HASH_ALGORITM_ADLER32 = 'adler32';
const HASH_ALGORITM_CRC32 = 'crc32':
const HASH_ALGORITM_CRC32B = 'crc32b';
const HASH_ALGORITM_HAVAL128_3 = 'haval128,3';
const HASH_ALGORITM_HAVAL160_3 = 'haval160,3';
const HASH_ALGORITM_HAVAL192_3 = 'haval192,3';
const HASH_ALGORITM_HAVAL224_3 = 'haval224,3';
const HASH_ALGORITM_HAVAL256_3 = 'haval256,3';
const HASH_ALGORITM_HAVAL128_4 = 'haval128,4';
const HASH_ALGORITM_HAVAL160_4 = 'haval160,4';
const HASH_ALGORITM_HAVAL192_4 = 'haval192,4';
const HASH_ALGORITM_HAVAL224_4 = 'haval224,4';
const HASH_ALGORITM_HAVAL256_4 = 'haval256,4';
const HASH_ALGORITM_HAVAL128_5 = 'haval128,5';
const HASH_ALGORITM_HAVAL160_5 = 'haval160,5';
const HASH_ALGORITM_HAVAL192_5 = 'haval192,5';
const HASH_ALGORITM_HAVAL224_5 = 'haval224,5';
const HASH_ALGORITM_HAVAL256_5 = 'haval256,5';
/**
* The chosen hash algoritm.
*/
public static $HASH_ALGORITM = self::HASH_ALGORITM_WHIRLPOOL;
/**
* The salt string.
*/
public static $SALT = 'salt_:)';
/**
* The position in the password where the salt should be inserted.
*/
public static $SALT_INDEX_START = 4;
/**
* The password.
*/
private $_password = '';
/**
* Create a password object.
* The password is hashed before it's setted in this class.
*
* @param string the password that should be hashed.
*/
public function __construct($password)
{
$this->_password = $this->_hash($password);
}
/**
* @return string the password is returned.
*/
public function getPassword()
{
return $this->_password;
}
/**
* Execute the hash algoritm.
*
* @param string the password.
* @return string the password hashed.
*/
protected function _hash($password)
{
$password = substr($password, 0, self::$SALT_INDEX_START) . self::$SALT . substr($password, self::$SALT_INDEX_START);
return hash(self::$HASH_ALGORITM, $password);
}
/**
* @return string the string representation of this password object. This gives the hashed password.
*/
public function __toString()
{
return $this->getPassword();
}
}