static function hmacsha256($data, $key) { if (function_exists('hash_hmac')) { return hash_hmac('sha256', $data, $key, true); } if (!class_exists('nanoSha2', false)) { require 'nanoSha2.php'; } $nanoSha2 = new nanoSha2(); $blocksize = 64; if (strlen($key) > $blocksize) { $key = pack('H*', $nanoSha2->hash($key, true)); } $key = str_pad($key, $blocksize, chr(0x0)); $ipad = str_repeat(chr(0x36), $blocksize); $opad = str_repeat(chr(0x5c), $blocksize); $hmac = pack('H*', $nanoSha2->hash(($key ^ $opad) . pack('H*', $nanoSha2->hash(($key ^ $ipad) . $data, true)), true)); return $hmac; }
/** * Main routine called from an application using this include. * * General usage: * require_once('sha256.inc.php'); * $hashstr = sha256('abc'); * * Note: * PHP Strings are limitd to (2^31)-1, so it is not worth it to * check for input strings > 2^64 as the FIPS180-2 defines. */ function _nano_sha256($str, $ig_func = true) { $obj = new nanoSha2((defined('_NANO_SHA2_UPPER')) ? true : false); return $obj->hash($str, $ig_func); }
/** * Generates a SHA256 using whatever methods are available. If no valid function can be found, the data will be returned unhashed. * * @param string $data - The data to encrypt. * @return string - Encrypted data. * @author Joseph Todd Parsons <*****@*****.**> */ function fim_sha256($data) { global $config; if (function_exists('hash') && in_array('sha256', hash_algos())) { return hash('sha256', $data); } elseif (function_exists('mhash') && defined('MHASH_SHA256')) { return mhash(MHASH_SHA256, $data); } else { // Otherwise, we use a third-party SHA256 library. Expect slowness. [TODO: Test] require 'functions/sha256.php'; // Require SHA256 class provided by NanoLink.ca. $obj = new nanoSha2(); $shaStr = $obj->hash($data); } }