public static function verifyHashedData($str) { try { // Explode the String into variables @(list($signature, $payload) = @explode('-', $str, 2)); // Validate the given signature against the one we create using the payload if ($signature && $payload && $signature === hash_hmac(self::$algorithm, self::base64UrlDecode($payload), self::$secret)) { // Store the data self::$data = json_decode(self::base64UrlDecode($payload), TRUE); // Data untaltered return TRUE; } // Data altered return FALSE; } catch (Exception $e) { // Unknown error occured return FALSE; } }
<?php // Require class file require_once 'libs/SecureHash.class.php'; // Get the instance $SecureHash = SecureHash::getInstance(); // Set the shared secret, algorithm and delimiter $SecureHash::setSharedSecret('328beab968f0faaec4c6bbd912aba013c929fd01'); $SecureHash::setAlgorithm('sha1'); $SecureHash::setDelimiter('-'); // Hashe the data and return as string $str = $SecureHash::generateHashedData(array('Hello' => 'World')); // Debug output var_dump($str); // Just verify, don't return the parsed data var_dump($SecureHash::verifyHashedData($str)); // Like verifyHashedData() but returns the parsed data var_dump($SecureHash::parseHashedData($str));