You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

77 lines
2.1 KiB

<?php
include("base32hex.php");
function dnsname($str){
// Doesnt handle \\.... encoding
$out = '';
foreach(explode('.', trim($str, '.') . '.') as $pp){
$out .= chr(strlen($pp));
$out .= $pp;
}
return($out);
}
function calculate_hash($host, $salt, $iter){
$saltbin = pack("H*", $salt);
$wf = dnsname($host);
$nsec3 = sha1($wf . $saltbin, true);
for($i = 0; $i < $iter; $i++){
$nsec3 = sha1($nsec3 . $saltbin, true);
}
return $nsec3;
}
function increment_hash($hash){
for($i = strlen($hash) - 1; $i >= 0; --$i){
$c = $hash[$i];
$c = chr(ord($c) + 1);
$hash[$i] = $c;
if(ord($c))
break;
}
return $hash;
}
function decrement_hash($hash){
for($i = strlen($hash) - 1; $i >= 0; --$i){
$c = $hash[$i];
$c = chr(ord($c) - 1);
$hash[$i] = $c;
if($c != chr(255))
break;
}
return $hash;
}
function hash_base32encode($hash){
return strtolower(base32hex_encode($hash));
}
$salt = "BC6222C640";
$iter = 5;
foreach(["simonvikstrom.se", "_assaas.simonvikstrom.se", "*.simonvikstrom.se"] as $domain){
$hash = calculate_hash($domain, $salt, $iter);
echo "$domain hash: ", hash_base32encode($hash), "\n";
echo "$domain hash before: ", hash_base32encode(decrement_hash($hash)), "\n";
echo "$domain hash after: ", hash_base32encode(increment_hash($hash)), "\n";
}
echo "\n";
echo bin2hex(increment_hash(str_repeat(chr(255), 20)));
echo "\n";
echo bin2hex(decrement_hash(str_repeat(chr(0), 20)));
/*
No wildcard
Powerdns returns :
hash(appex (SOA)) -> increment_hash(hash(appex (SOA)))
decrement_hash(hash(query)) -> increment_hash(hash(query))
decrement_hash(hash(*.appex (SOA))) -> increment_hash(hash(*.appex (SOA)))
Wildcard
hash(appex (query)) -> increment_hash(hash(query))
*/