Created
March 1, 2013 15:33
-
-
Save printminion/5065424 to your computer and use it in GitHub Desktop.
function for signing values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class SecureHash | |
| { | |
| const SECRET = 'PUT_YOUR_SECRET_HERE'; | |
| const SEPARATOR = '|'; | |
| /** | |
| * hash string | |
| * | |
| * @param string $valueToSign value to sign | |
| * @param string $secretValue additional sign secret | |
| * @return string | |
| */ | |
| private static function hashString($valueToSign, $secretValue = null) | |
| { | |
| return hash_hmac('sha1', $valueToSign, $secretValue . self::SECRET); | |
| } | |
| /** | |
| * generate signed value | |
| * | |
| * @param $valueToSign | |
| * @param string $secretValue additional sign secret | |
| * @return string | |
| */ | |
| static function makeSecureValue($valueToSign, $secretValue = null) | |
| { | |
| return $valueToSign . self::SEPARATOR . self::hashString($valueToSign, $secretValue); | |
| } | |
| /** | |
| * validate signed value | |
| * @param string $valueToValidate | |
| * @param string $secretValue additional sign secret | |
| * @return mixed | |
| */ | |
| static function checkSecureValue($valueToValidate, $secretValue = null) | |
| { | |
| $value = explode(self::SEPARATOR, $valueToValidate); | |
| if (count($value) != 2) { | |
| return; | |
| } | |
| $value = $value[0]; | |
| if ($valueToValidate == self::makeSecureValue($value, $secretValue)) { | |
| return $value; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment