okay boys and girls.. here it is, the SHA256 hashing algorithm implemenation. Usage information and description are in the code. Please note: I have only been able to test this against relatively small messages. There are testing functions provided at the bottom of the code which is after a "REMOVAL ALL FUNCTIONS AFTER THIS WHEN NOT TESTING" comment.

for($i=0;$i<32;$i++){// sum of the bits at $i$j=(($T>>$i)& 1)+(($a>>$i)& 1)+$c;// carry of the bits at $i$c=($j>> 1)&1;// strip the carry$j&=1;// clear the bit$T&= ~(1 <<$i);// set the bit$T|=$j<<$i;}}

$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',);

for($i=0;$i<32;$i++){// sum of the bits at $i$j=(($T>>$i)& 1)+(($a>>$i)& 1)+$c;// carry of the bits at $i$c=($j>> 1)&1;// strip the carry$j&=1;// clear the bit$T&= ~(1 <<$i);// set the bit$T|=$j<<$i;}}

$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',);

The following shows the output for a test against the string 'abc' using the above debugging version of the SHA256 class.

The first piece you will see is the results of the padding against a message block (512 bits - 64 bytes). an example of what this should be is provided in the specification linked to in the code above.

The next section is the compression algrorithm output registers, starting with the initial data of the starting constants. an example of what this should be is provided in the specification linked to in the code above.

The section after that is the W temporary storage variable's contents.

repeat from number 2 for each chunk in the message.

Notice that the 'DONE' line in the final compression algorithm output is the final hash broken into 32-bit chunks.

for($i=0;$i<32;$i++){// sum of the bits at $i$j=(($T>>$i)& 1)+(($a>>$i)& 1)+$c;// carry of the bits at $i$c=($j>> 1)&1;// strip the carry$j&=1;// clear the bit$T&= ~(1 <<$i);// set the bit$T|=$j<<$i;}}

$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',);

for($i=0;$i<32;$i++){// sum of the bits at $i$j=(($T>>$i)& 1)+(($a>>$i)& 1)+$c;// carry of the bits at $i$c=($j>> 1)&1;// strip the carry$j&=1;// clear the bit$T&= ~(1 <<$i);// set the bit$T|=$j<<$i;}}

$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',
*/);

Of course hash() and hash_file() functions can use many other hash algorithms such as:md2md4md5sha1sha256sha384sha512ripemd128ripemd160ripemd256ripemd320whirlpooltiger128,3tiger160,3tiger192,3tiger128,4tiger160,4tiger192,4snefrugostadler32crc32crc32bhaval128,3haval160,3haval192,3haval224,3haval256,3haval128,4haval160,4haval192,4haval224,4haval256,4haval128,5haval160,5haval192,5haval224,5 haval256,5

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum