Skip to content

Instantly share code, notes, and snippets.

@mittsh
Created November 3, 2017 06:50
Show Gist options
  • Save mittsh/b52b2f363ff3b50fbffd4da12962965b to your computer and use it in GitHub Desktop.
Save mittsh/b52b2f363ff3b50fbffd4da12962965b to your computer and use it in GitHub Desktop.
<?php
function getHashedId($id) {
$random = base64_encode(random_bytes(32));
$stringToSign = $id . "\0" . $random;
$hash = hash('sha256', $stringToSign);
return base64_encode($hash . "\0" . $random);
}
function verifyHashedId($id, $hashedId) {
$a = explode("\0", base64_decode($hashedId));
$hash = $a[0];
$random = $a[1];
$stringToSign = $id . "\0" . $random;
return hash('sha256', $stringToSign) === $hash;
}
function getContactId($contact, $hashedId) {
foreach ($contact as $c) {
if (verifyHashedId($c['Id'], $hashedId)) {
return $c['Id'];
}
}
return null;
}
function getContactLink($c) {
$content = $c['Email']."\0".getHashedId($id)."\0".time()."\0".base64_encode(random_bytes(32));
$contentSigned = $content."\n". hash_hmac('sha256',$content,getenv('SECRET'));
return base64_encode($contentSigned);
}
function generateTmpLinks($contact) {
$links = array();
foreach ($contact as $c) {
$links[] = getContactLink($c);
}
return $links;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment