This is a piece of code I found when doing some fall cleaning that I wrote for RAC a while back, but my bid wasn't taken. This is meant to create a "code" based on a domain, and can be verified by checking the domain against the code.
<?PHP
//============================================================
//#region TESTING CODE
echo "Domain: www.google.com<br />";
echo "Code: " . makeCode("www.google.com") . "<br />";
echo "Success: " . (bool) verifyCode("www.google.com", makeCode("www.google.com"));
echo "<br />";
echo "<br />";
echo "Domain: www.yahoo.com<br />";
echo "Code: " . makeCode("www.yahoo.com") . "<br />";
echo "Success: " . (bool) verifyCode("www.yahoo.com", makeCode("www.yahoo.com"));
echo "<br />";
echo "<br />";
echo "Domain: www.amazon.com<br />";
echo "Code: " . makeCode("www.amazon.com") . "<br />";
echo "Success: " . (bool) verifyCode("www.amazon.com", makeCode("www.amazon.com"));
echo "<br />";
echo "<br />";
//#endregion TESTING CODE
//============================================================
function makeCode($arg_domain)
{
$hash = $arg_domain; // Bring in the original string
$hash = sha1($hash, true); // SHA-1 hash the whole string
$hash = hashChars($hash); // Hash it, character by character
$hash = md5($hash, true); // MD5 hash the whole string
$hash = hashChars($hash); // Hash it, character by character
$hash = md5($hash, false); // Hash it again, this time to text
$result = "";
for($i = 0; $i < 64 /* length of MD5 hash */; $i+=2) // This takes every fourth character
{ // from the string, and then makes
$result .= substr($hash, $i, 1); // that the result
}
return strtoupper($result); // This capitalizes the final result
}
function verifyCode($domain, $code)
{
return (makeCode($domain) == $code);
}
// ----------------------------------------------------
// The below code is called by the above two methods to
// provide various encryption techniques.
// ----------------------------------------------------
function hashChars($arg_data)
{
$result = "";
for($i = 0; $i < strlen($arg_data)-4; $i+=4)
{
$result .= substr(sha1(substr($arg_data, $i, 1), true), 1, 1);
$result .= substr(sha1(substr($arg_data, $i+1, 1), true), 1, 1);
$result .= substr(sha1(substr($arg_data, $i+2, 1), true), 1, 1);
$result .= substr(sha1(substr($arg_data, $i+3, 1), true), 1, 1);
}
return $result;
}
?>