/** * Replace hash() * * @category PHP * @package PHP_Compat * @license LGPL - http://www.gnu.org/licenses/lgpl.html * @copyright 2004-2007 Aidan Lister <*****@*****.**>, Arpad Ray <*****@*****.**> * @link http://php.net/function.hash * @author revulo <*****@*****.**> * @since PHP 5.1.2 * @require PHP 4.0.0 (user_error) */ function php_compat_hash($algo, $data, $raw_output = false) { $algo = strtolower($algo); switch ($algo) { case 'md5': $hash = md5($data); break; case 'sha1': if (!function_exists('sha1')) { require dirname(__FILE__) . '/sha1.php'; } $hash = sha1($data); break; case 'sha256': require_once dirname(__FILE__) . '/sha256.php'; $hash = SHA256::hash($data); break; default: user_error('hash(): Unknown hashing algorithm: ' . $algo, E_USER_WARNING); return false; } if ($raw_output) { return pack('H*', $hash); } else { return $hash; } }
/** * Creates SHA256 hash to obfuscate ips * * @param $ip ip address to be hashed * @return string sha256-hashed ip */ function hash_it_the_oas_way($ip) { global $config; $str = $ip . $config['hashsalt']; // hashen (SHA256) if (function_exists('mhash')) { // mhash-Extension geladen return bin2hex(mhash(MHASH_SHA256, $str)); } elseif (function_exists('hash')) { // hash-Extension geladen return hash('sha256', $str); // untested } else { // native PHP-Implementation als (langsame) Alternative / Fallback require_once 'sha256.php'; return SHA256::hash($str); // untested } }
function testSpeedHash($it = 10) { $it = intval($it); if ($it === 0) { $it = 10; } set_time_limit(-1); echo '<pre>' . "\n"; $test = array('' => 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'abc' => 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', 'message digest' => 'f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650', 'secure hash algorithm' => 'f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d', 'SHA256 is considered to be safe' => '6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' => '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1', 'For this sample, this 63-byte string will be used as input data' => 'f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342', 'This is exactly 64 bytes long, not counting the terminating byte' => 'ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8'); foreach ($test as $str => $hash) { echo 'Testing ' . var_export($str, true) . "\n"; echo 'Start time: ' . date('Y-m-d H:i:s') . "\n"; if ($it > 1) { list($s1, $s2) = explode(' ', microtime()); $o = SHA256::hash($str); list($e1, $e2) = explode(' ', microtime()); echo 'estimated time to perform test: ' . ($e2 - $s2 + $e1 - $s1) * $it . ' seconds for ' . $it . ' iterations.' . "\n"; } $t = 0; for ($x = 0; $x < $it; $x++) { list($s1, $s2) = explode(' ', microtime()); $o = SHA256::hash($str); list($e1, $e2) = explode(' ', microtime()); $t += $e2 - $s2 + $e1 - $s1; } echo var_export($o, true) . ' == ' . var_export($hash, true) . ' ' . (strcasecmp($o, $hash) == 0 ? 'PASSED' : 'FAILED') . "\n"; echo 'processing took ' . $t / $it . ' seconds.' . "\n\n\n"; } echo '</pre>'; }
function strhash($str, $salt = true) { if ($salt === true) { $str = md5($str) . $str; } elseif ($salt !== false) { $str = $salt . $str; } if (phpversion() >= '5.1.2' && @extension_loaded('pecl')) { return hash('sha256', $str); } else { import('lib/sha256'); return SHA256::hash($str); } }