Code: Select all
<?php
/*******************************************************************************
*
* SHA256 static class for PHP4
* implemented by feyd _at_ devnetwork .dot. net
* specification from http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf
*
* (C) Copyright 2005 Developer's Network. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
*----- Source Control Information ---------------------------------------------
*
* $Workfile: test_sha256.php $
* $Author: Feyd $
* $JustDate: 05.04.06 $
* $Revision: 3 $
*
* $Header: /inc/test_sha256.php 3 05.04.06 2:57p Feyd $
*
******************************************************************************/
require_once( 'hash_sha256.php' );
// format a string into 4 byte hex chunks
function hexerize($str)
{
$n = 0;
$b = 0;
if(is_array($str))
{
reset($str);
$o = 'array(' . sizeof($str) . ')::' . "\n\n";
while($s = current($str))
{
$o .= hexerize($s);
next($str);
}
$o .= 'end array;'."\n";
}
else
{
if(is_integer($str) || is_float($str))
$str = pack('N',$str);
$o = 'string(' . strlen($str) . ')' . "::\n";
for($i = 0, $j = strlen($str); $i < $j; $i++, $b = $i % 4)
{
$o .= sprintf('%02X', ord($str{$i}));
// only process when 32-bits have passed through
if($i != 0 && $b == 3)
{
// process new line points
if($n == 3)
$o .= "\n";
else
$o .= ' ';
++$n;
$n %= 4;
}
}
}
return $o . "\n";
}
class testSHA256
{
function runTests( $run = null )
{
if( $run === null )
{
$run = array
(
'test1',
'test2',
'testSum',
array('testSpeedHash', 10),
array('testOutput', 'hex', 10),
array('testOutput', 'HEX', 10),
array('testOutput', 'bin', 10),
array('testOutput', 'bit', 10),
array('testFiles', 'hex', 10),
array('testFiles', 'HEX', 10),
array('testFiles', 'bin', 10),
array('testFiles', 'bit', 10),
array('testURLs', 'hex', 1),
array('testURLs', 'HEX', 1),
array('testURLs', 'bin', 1),
array('testURLs', 'bit', 1),
);
}
echo '<html><body color="#000000" bgcolor="#FFFFFF">';
foreach( $run as $k => $v )
{
if( is_string( $v ) )
{
if( is_callable( array('testSHA256', $v) ) )
{
echo 'running testSHA256::' . $v . '()' . "\n\n";
testSHA256::$v();
}
else
{
trigger_error('testSHA256::' . $v . '() is an unknown function.', E_USER_NOTICE);
}
}
elseif( is_array( $v ) )
{
if( is_callable( array('testSHA256', $v[0]) ) )
{
$f = $v[0];
array_shift($v);
echo 'running testSHA256::' . $f . '(' . var_export($v,true) . ')' . "\n\n";
call_user_func_array( array('testSHA256', $f), $v );
}
else
{
trigger_error('testSHA256::' . $v[0] . '() is an unknown function.', E_USER_NOTICE);
}
}
else
{
trigger_error('testSHA256(): unknown data at key ' . $k, E_USER_NOTICE);
}
}
echo '</body></html>';
}
// testing functions
function test1()
{
$it = 1;
echo '<pre>';
$test = array('abc','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
foreach($test as $str)
{
echo 'Testing ' . var_export($str,true) . "\n";
list($s1,$s2) = explode(' ', microtime());
for($x = 0; $x < $it; $x++)
$data =& new SHA256Data('', $str);
list($e1,$e2) = explode(' ', microtime());
echo hexerize($data->message->chunk);
echo hexerize($data->hash);
echo 'processing took ' . (($e2 - $s2 + $e1 - $s1) / $it) . ' seconds.' . "\n\n\n";
}
echo '</pre>';
}
function test2()
{
$it = 1;
echo '<pre>';
$test = array('abc','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
foreach($test as $str)
{
echo 'Testing ' . var_export($str,true) . "\n";
list($s1,$s2) = explode(' ', microtime());
for($x = 0; $x < $it; $x++)
$o = SHA256::hash($str);
list($e1,$e2) = explode(' ', microtime());
echo $o . "\n";
echo 'processing took ' . (($e2 - $s2 + $e1 - $s1) / $it) . ' seconds.' . "\n\n\n";
}
echo '</pre>';
}
function testSum()
{
echo '<pre>';
echo SHA256::sum(1,2,3,4,5,6,7,8,9,10);
echo '</pre>';
}
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 testOutput($mode = 'hex', $it = 10)
{
$modes = array( 'hex' => 16, 'HEX' => 16, 'bin' => 10, 'bit' => 2 );
static $hex = '0123456789abcdef';
static $bit = array(
'0' => '0000',
'1' => '0001',
'2' => '0010',
'3' => '0011',
'4' => '0100',
'5' => '0101',
'6' => '0110',
'7' => '0111',
'8' => '1000',
'9' => '1001',
'a' => '1010',
'b' => '1011',
'c' => '1100',
'd' => '1101',
'e' => '1110',
'f' => '1111',
);
$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',
);
if( isset($modes[$mode]) )
{
foreach($test as $k => $v)
{
switch( $mode )
{
case 'HEX':
$test[$k] = strtoupper($v);
break;
case 'bin':
$test[$k] = '';
for($i = 0, $j = strlen($v); $i < $j; $i += 2)
{
$test[$k] .= chr( (strpos($hex,$v{$i}) << 4) | (strpos($hex,$v{$i + 1})) );
}
break;
case 'bit':
$test[$k] = '';
for($i = 0, $j = strlen($v); $i < $j; $i++)
{
$test[$k] .= $bit[$v{$i}];
}
break;
case 'hex':
default:
break;
}
}
}
else
{
$mode = 'hex';
}
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, $mode);
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, $mode);
list($e1,$e2) = explode(' ', microtime());
$t += $e2 - $s2 + $e1 - $s1;
}
echo var_export($o,true) . ' ==
' . var_export($hash,true) . ' ' . ($o === $hash ? 'PASSED' : 'FAILED') . "\n";
echo 'processing took ' . ($t / $it) . ' seconds.' . "\n\n\n";
}
echo '</pre>';
}
function testFiles( $mode = 'hex', $it = 10 )
{
$modes = array( 'hex' => 16, 'HEX' => 16, 'bin' => 10, 'bit' => 2 );
static $hex = '0123456789abcdef';
static $bit = array(
'0' => '0000',
'1' => '0001',
'2' => '0010',
'3' => '0011',
'4' => '0100',
'5' => '0101',
'6' => '0110',
'7' => '0111',
'8' => '1000',
'9' => '1001',
'a' => '1010',
'b' => '1011',
'c' => '1100',
'd' => '1101',
'e' => '1110',
'f' => '1111',
);
$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',
);
if( isset($modes[$mode]) )
{
foreach($test as $k => $v)
{
switch( $mode )
{
case 'HEX':
$test[$k] = strtoupper($v);
break;
case 'bin':
$test[$k] = '';
for($i = 0, $j = strlen($v); $i < $j; $i += 2)
{
$test[$k] .= chr( (strpos($hex,$v{$i}) << 4) | (strpos($hex,$v{$i + 1})) );
}
break;
case 'bit':
$test[$k] = '';
for($i = 0, $j = strlen($v); $i < $j; $i++)
{
$test[$k] .= $bit[$v{$i}];
}
break;
case 'hex':
default:
break;
}
}
}
else
{
$mode = 'hex';
}
foreach($test as $str => $hash)
{
$fn = tempnam('.','SHA_tmp');
$fp = fopen( $fn, 'w' );
fwrite( $fp, $str );
echo 'Temp filename: ' .$fn . ' (' . __LINE__ . ")\n";
fclose($fp);
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::hashFile($fn, $mode);
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::hashFile($fn, $mode);
list($e1,$e2) = explode(' ', microtime());
$t += $e2 - $s2 + $e1 - $s1;
}
echo var_export($o,true) . ' ==
' . var_export($hash,true) . ' ' . ($o === $hash ? 'PASSED' : 'FAILED') . "\n";
echo 'processing took ' . ($t / $it) . ' seconds.' . "\n\n\n";
unlink($fn);
}
echo '</pre>';
}
function testURLs( $mode = 'hex', $it = 1 )
{
$modes = array( 'hex' => 16, 'HEX' => 16, 'bin' => 10, 'bit' => 2 );
static $hex = '0123456789abcdef';
static $bit = array(
'0' => '0000',
'1' => '0001',
'2' => '0010',
'3' => '0011',
'4' => '0100',
'5' => '0101',
'6' => '0110',
'7' => '0111',
'8' => '1000',
'9' => '1001',
'a' => '1010',
'b' => '1011',
'c' => '1100',
'd' => '1101',
'e' => '1110',
'f' => '1111',
);
$it = intval($it);
if($it == 0)
$it = 10;
set_time_limit(60);
echo '<pre>' . "\n";
$test = array(
''=>array('http://www.tatzu.net/test/blank.txt','e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'),
'abc'=>array('http://tatzu.net/test/abc.php','ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'),
'message digest'=>array('test.tatzu.net/3.html','f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650'),
'secure hash algorithm'=>array('http://test.tatzu.com/secure.js','f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d'),
'SHA256 is considered to be safe'=>array('http://www.TATZU.NeT/test/','6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630'),
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'=>array('wWw.tAtZu.nEt/test/asDF.php','248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'),
'For this sample, this 63-byte string will be used as input data'=>array('http://www.tatzu.net/test/far.txt','f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342'),
'This is exactly 64 bytes long, not counting the terminating byte'=>array('www.tatzu.net/test/fir.html','ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8'),
);
if( isset($modes[$mode]) )
{
foreach($test as $k => $v)
{
switch( $mode )
{
case 'HEX':
$test[$k][1] = strtoupper($v[1]);
break;
case 'bin':
$test[$k][1] = '';
for($i = 0, $j = strlen($v[1]); $i < $j; $i += 2)
{
$test[$k][1] .= chr( (strpos($hex,$v[1]{$i}) << 4) | (strpos($hex,$v[1]{$i + 1})) );
}
break;
case 'bit':
$test[$k][1] = '';
for($i = 0, $j = strlen($v[1]); $i < $j; $i++)
{
$test[$k][1] .= $bit[$v[1]{$i}];
}
break;
case 'hex':
default:
break;
}
}
}
else
{
$mode = 'hex';
}
foreach($test as $str => $arr)
{
list($url,$hash) = $arr;
echo 'Testing ' . var_export($str,true) . "\n";
echo 'URL: ' . $url . "\n";
echo 'Start time: ' . date('Y-m-d H:i:s') . "\n";
if($it > 1)
{
list($s1,$s2) = explode(' ', microtime());
$o = SHA256::hashURL($url, $mode);
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::hashURL($url, $mode);
list($e1,$e2) = explode(' ', microtime());
$t += $e2 - $s2 + $e1 - $s1;
}
echo var_export($o,true) . ' ==
' . var_export($hash,true) . ' ' . ($o === $hash ? 'PASSED' : 'FAILED') . "\n";
echo 'processing took ' . ($t / $it) . ' seconds.' . "\n\n\n";
}
echo '</pre>';
}
}
/*****************************************************************************
* How the syntax of this test works:
* Each element of the array (optionally) passed to testSHA256::runTests()
* is static method call information for methods within testSHA256.
*
* A simple string is used for method calls that will use no arguments, or
* all default arguments of the method specified. An array is used for those
* methods that need some arguments passed to them. The first element is the
* method to call. All other elements are passed directly to the method.
* Methods are called in order of how they are stored in this array.
****************************************************************************/
$tests = array
(
'test1',
'test2',
'testSum',
array('testSpeedHash', 10),
array('testOutput', 'hex', 1),
array('testOutput', 'HEX', 1),
array('testOutput', 'bin', 1),
array('testOutput', 'bit', 1),
array('testFiles', 'hex', 1),
array('testFiles', 'HEX', 1),
array('testFiles', 'bin', 1),
array('testFiles', 'bit', 1),
array('testURLs', 'hex', 1),
array('testURLs', 'HEX', 1),
array('testURLs', 'bin', 1),
array('testURLs', 'bit', 1),
);
testSHA256::runTests( $tests );
/*****************************************************************************
* $History: test_sha256.php $
*
* ***************** Version 3 *****************
* User: Feyd Date: 05.04.06 Time: 2:57p
* Updated in $/inc
* adjust/modify some documentation for release of 1.1.0
*
* ***************** Version 2 *****************
* User: Feyd Date: 05.04.05 Time: 1:26a
* Updated in $/inc
* hashURL() finished.
*
* ***************** Version 1 *****************
* User: Feyd Date: 05.04.04 Time: 12:40a
* Created in $/inc
* initial add
*****************************************************************************/
/* EOF :: Document Settings: tab:4; */
?>