Skip to content

Instantly share code, notes, and snippets.

@dunglas
Last active January 11, 2016 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dunglas/a4d8fa31fb04cc42eaaf to your computer and use it in GitHub Desktop.
Save dunglas/a4d8fa31fb04cc42eaaf to your computer and use it in GitHub Desktop.
public static function generateHash($value, $namespace = '')
{
$hash = hash_init('sha256');
$queue = array(array($namespace => $value));
for ($i = 0; isset($queue[$i]); ++$i) {
foreach ($queue[$i] as $k => $v) {
if (is_object($v)) {
hash_update($hash, $k.'=>'.spl_object_hash($v).',';
} elseif (is_array($v)) {
$queue[] = $v;
} else {
hash_update($hash, $k.'=>'.$v.',');
}
}
unset($queue[$i]);
hash_update($hash, "\n");
}
return hash_final($hash);
}
public static function generateHash($value, $namespace = '')
{
$queue = array(array($namespace => $value));
for ($i = 0; isset($queue[$i]); ++$i) {
foreach ($queue[$i] as $k => $v) {
if (is_object($v)) {
$queue[$i][$k] = spl_object_hash($v);
} elseif (is_array($v)) {
unset($queue[$i][$k]);
$queue[] = $v;
}
}
}
return hash('sha256', serialize($queue));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment