Skip to content

Instantly share code, notes, and snippets.

@printminion
Created March 1, 2013 15:33
Show Gist options
  • Select an option

  • Save printminion/5065424 to your computer and use it in GitHub Desktop.

Select an option

Save printminion/5065424 to your computer and use it in GitHub Desktop.
function for signing values
<?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