I was in need of a PHP class to encrypt hash and verify passwords using salted sha1 hashes. Here it is, quick but not too dirty:
<?php class SSHA { public static function newSalt() { return chr(rand(0,255)).chr(rand(0,255)).chr(rand(0,255)).chr(rand(0,255)).chr(rand(0,255)).chr(rand(0,255)).chr(rand(0,255)).chr(rand(0,255)); } public static function hash($pass,$salt) { return '{SSHA}'.base64_encode(sha1($pass.$salt,true).$salt); } public static function getSalt($hash) { return substr(base64_decode(substr($hash,6)),20); } public static function newHash($pass) { return self::hash($pass,self::newSalt()); } public static function verifyPassword($pass,$hash) { return $hash == self::hash($pass,self::getSalt($hash)); } }
Tags: dovecot, encryption, hash, OOP, openLDAP, passwd, PHP, RFC2307, sha1, ssha
No comments
Comments feed for this article
Trackback link: https://ten-fingers-and-a-brain.com/2009/08/ssha-php/trackback/