Source for file secure_hash.class.php
Documentation is available at secure_hash.class.php
* Secure password hashing class
* @author Julius Beckmann
* @link http://juliusbeckmann.de/classes/secure_hash/
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* Secure password hashing class. A simple and easy to use class for secure
* password hashing. It is using random Salts and multiple hashing with random
* iterations. The class can be easily extended with own hashing methods.
* @name Secure password hashing class
* @version v0.1_2009.09.18
* Copyright 2009 Julius Beckmann
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* Check available _hash_* methods
* Delimiter used in formated hash
* Just using special latin chars here so we wont break charset and avoid
* creating not secure salts. NO " and ' to avoid breaking queries.
* Global salt which is NOT stored inside formated hash
* but needed to check integrity of hash.
* This is a further security method to avoid hashes being cracked.
* Idea is to save salt in database, as well as in a config file or just here.
* A attacker would need access to both to gain enough data to crack these
* hashes. Changing this value leads to incorrect hashes.
* Just define it once and keep it.
* Keep this at least at 10 to keep hashlength static
* Keep this under 100 to keep hashlength static
* Flag if permutations should be used
* Add more rules to make hashing even more complex.
* Every rule gets applied
'0123456789abcdef|7d15b9f3e60a82c4',
'0123456789abcdef|b294cd1e6a038f57');
// --- PUBLIC Functions ---
* Hashes a string and returns the formated hash.
* @param string $pass string to hash
* @param string $salt salt to use, default false
* @param integer $iter iterations to do, default false
* @return string hashed string
function hash($pass, $salt= false, $iter= false) {
// Generate salt if unspecified
// Random iterations if unspecified
// Check hashing function
die('ERROR: The hashing-method "'. $method. '" is NOT callable!');
$hash = $this->$method($pass, $salt. $this->salt_global, $iter);
* Checks if a formated hash is equal to a password
* @param string $hash_formated hash to use
* @param string $pass password to check
* @return boolean true is hash == pass
function check($hash_formated, $pass) {
// Check if first and last char of formated string are our delimiter
$len = strlen($hash_formated);
if($len > 1 && $hash_formated[0] == $this->delimiter
&& $hash_formated[$len- 1] == $this->delimiter) {
list (,$hashmethod, $salt, $iter, $hash)
// Check hashing function
$method = '_hash_'. $hashmethod;
die('ERROR: The hashing-method "'. $method. '" is NOT callable!');
$ret = ($hash == $this->$method($pass, $salt. $this->salt_global, $iter));
* Returns a new permutaion which can be used in this class
* Permutation format is abc|cab - a->c && b->a && c->b
* @param $b string base for permutation
* @return string permutation for this class
// --- PRIVATE Functions ---
// Remove delimiter from salt chars
$char_count = strlen($chars)- 1;
$salt .= $chars[rand(0, $char_count)];
* Plugin sha1 hashing method
* @param string $str string to hash
* @param string $salt salt to use
* @param int $iter iterations to do
for($i= 0; $i< $iter; ++ $i)
* Plugin md5 hashing method
* @param string $str string to hash
* @param string $salt salt to use
* @param int $iter iterations to do
for($i= 0; $i< $iter; ++ $i)
* Permutation string with global rules
* @param string string to permutate
* @return string permutated string
// Apply each permutation
* Permutate a string with given rule
* @param string $str referece (faster) for the string to permutate
* @param string $perm rule in form of 123|312
* @param bool $dir direction to permutate. Default: true = forward
* @param integer $iter number of iterations to do. Default: depends on
* last char and length of $perm.
* @return bool always true.
function _permutate(&$str, &$perm, $dir= true, $iter= false) {
list ($from, $to) = explode('|',$perm, 2);
list ($to, $from) = explode('|',$perm, 2);
return false; // Rule is incorrect - stop here.
// Get a new iterationcount depending on last rule char
$iter = (ord($perm[strlen($perm)- 1]) % ($len_base/ 2));
// Create permutation array
for($i= 0; $i< $len_base; ++ $i)
if(isset ($p[$str[$i]])) //This will keep chars that are not in our rule.
* Example code how secure_hash can be easily extended for own hashing methods.
* New default hashing function
* Plugin own hashing method
* @param string $str string to hash
* @param string $salt salt to use
* @param int $iter iterations to do
for($i= 0; $i< $iter; ++ $i)
|