function hash($str, $mode = 'hex') { static $modes = array('hex', 'bin', 'bit'); $ret = false; if (!in_array(strtolower($mode), $modes)) { trigger_error('mode specified is unrecognized: ' . $mode, E_USER_WARNING); } else { $data = new SHA256Data($str); SHA256::compute($data); $func = array('SHA256', 'hash' . $mode); if (is_callable($func)) { $func = 'hash' . $mode; $ret = SHA256::$func($data); } else { trigger_error('SHA256::hash' . $mode . '() NOT IMPLEMENTED.', E_USER_WARNING); } } return $ret; }
* Test SHA-1 with one official test vector and custom input. * Vectors from: http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/ */ $sha1 = SHA1::compute($input); $sha1tv = SHA1::compute(""); $sha1hmac = SHA1::computeHMAC("1234567890123456", $input); // print "SHA-1 from otv is ok: " . bool_str(Base16::encode($sha1tv) == "da39a3ee5e6b4b0d3255bfef95601890afd80709") . "<br/>\n"; print "SHA-1 HMAC in UTF-8: " . Base16::encode($sha1hmac) . "<br/>\n"; print "SHA-1 in UTF-8: " . Base16::encode($sha1) . "<br/><br/>\n"; /** * Test SHA-256 with one official test vector and custom input. * Vectors from: http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/ */ $sha256 = SHA256::compute($input); $sha256tv = SHA256::compute(""); $sha256hmac = SHA256::computeHMAC("1234567890123456", $input); // print "SHA-256 from otv is ok: " . bool_str(Base16::encode($sha256tv) == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") . "<br/>\n"; print "SHA-256 HMAC in UTF-8: " . Base16::encode($sha256hmac) . "<br/>\n"; print "SHA-256 in UTF-8: " . Base16::encode($sha256) . "<br/><br/>\n"; /** * Test ARC4 with one official test vector and custom input. * Vectors from: http://reikon.us/arc4 */ $arc4tvk = Base16::decode("0123456789abcdef"); $arc4tvt = Base16::decode("0123456789abcdef"); $arc4tve = ARC4::encrypt($arc4tvk, $arc4tvt); $arc4tvd = ARC4::decrypt($arc4tvk, $arc4tve); // $arc4k = "1234567890123456";