diff options
author | Kevin Chabowski <kevin@kch42.de> | 2012-01-05 14:43:33 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2012-01-05 14:43:33 +0100 |
commit | c4cc87d9d1557ddd4cae4b06b79696712f61a2ad (patch) | |
tree | 081b31be890c484040a63f626063e846ef583680 /r7r_repo/pwhash.php | |
parent | e99281261767710f96b9967a0ab03a3ea24a05e0 (diff) | |
download | r7r-repo-c4cc87d9d1557ddd4cae4b06b79696712f61a2ad.tar.gz r7r-repo-c4cc87d9d1557ddd4cae4b06b79696712f61a2ad.tar.bz2 r7r-repo-c4cc87d9d1557ddd4cae4b06b79696712f61a2ad.zip |
New directory hierachy. index and setup implemented.
Diffstat (limited to 'r7r_repo/pwhash.php')
-rw-r--r-- | r7r_repo/pwhash.php | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/r7r_repo/pwhash.php b/r7r_repo/pwhash.php new file mode 100644 index 0000000..8ec4762 --- /dev/null +++ b/r7r_repo/pwhash.php @@ -0,0 +1,74 @@ +<?php +/* + * File: ratatoeskr/sys/pwhash.php + * + * Hashing passwords + * + * License: + * This file is part of Ratatöskr. + * Ratatöskr is licensed unter the MIT / X11 License. + * See "ratatoeskr/licenses/ratatoeskr" for more information. + */ + +/* + * Class: PasswordHash + * Contains static functions for password hashes. + * Is just used as a namespace, can not be created. + * + * It should be fairly difficult to break these salted hashes via bruteforce attacks. + */ +class PasswordHash +{ + private function __construct() {} /* Prevent construction */ + + private static $saltlen_min = 20; + private static $saltlen_max = 30; + private static $iterations_min = 200; + private static $iterations_max = 1000; + + private static function hash($data, $salt, $iterations) + { + $hash = $data . $salt; + for($i = $iterations ;$i--;) + $hash = sha1($data . $hash . $salt, (bool) $i); + return $iterations . '$' . bin2hex($salt) . '$' . $hash; + } + + /* + * Function: create + * Create a password hash string. + * + * Parameters: + * $password - The password (or other data) to hash. + * + * Returns: + * The salted hash as a string. + */ + public static function create($password) + { + $salt = ""; + $saltlen = mt_rand(self::$saltlen_min, self::$saltlen_max); + for($i = 0; $i < $saltlen; $i++) + $salt .= chr(mt_rand(0,255)); + return self::hash($password, $salt, mt_rand(self::$iterations_min, self::$iterations_max)); + } + + /* + * Function: validate + * Validate a salted hash. + * + * Parameters: + * $password - The password to test. + * $pwhash - The hash to test against. + * + * Returns: + * True, if $password was correct, False otherwise. + */ + public static function validate($password, $pwhash) + { + list($iterations, $hexsalt, $hash) = explode('$', $pwhash); + return self::hash($password, pack("H*", $hexsalt), $iterations) == $pwhash; + } +} + +?> |