Skip to content

Commit 57335a8

Browse files
committed
Added NSEC3 hash generation and error handling for the UNPACK function
1 parent 129404c commit 57335a8

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

DNS/dnsProtocol.php

+69-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function __construct($logging = false)
5151
$this->timeout=60;
5252
$this->udp=false;
5353
$this->types=new DNSTypes();
54+
set_error_handler(array($this,'error_handler'));
5455
$this->writelog("dnsProtocol Class Initialised");
5556
}
5657

@@ -62,7 +63,16 @@ function __destruct()
6263
}
6364
}
6465

65-
66+
function error_handler($errno = 0, $errstr = null, $errfile = null, $errline = null)
67+
{
68+
// If error is suppressed with @, don't throw an exception
69+
if (error_reporting() === 0)
70+
{
71+
return true; // return true to continue through the others error handlers
72+
}
73+
throw new DnsException('Found '.$errstr.' in line '.$errline.' of '.$errfile, $errno, null);
74+
}
75+
6676

6777
function Query($question,$type="A")
6878
{
@@ -345,4 +355,62 @@ function registrynameservers($tld)
345355
return $dnsservers;
346356
}
347357

358+
function base32encode($input, $padding = true) {
359+
360+
$map = array(
361+
'0', '1', '2', '3', '4', '5', '6', '7', // 7
362+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f', // 15
363+
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 23
364+
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 31
365+
'=' // padding char
366+
);
367+
368+
if(empty($input)) return "";
369+
$input = str_split($input);
370+
$binaryString = "";
371+
for($i = 0; $i < count($input); $i++) {
372+
$binaryString .= str_pad(base_convert(ord($input[$i]), 10, 2), 8, '0', STR_PAD_LEFT);
373+
}
374+
$fiveBitBinaryArray = str_split($binaryString, 5);
375+
$base32 = "";
376+
$i=0;
377+
while($i < count($fiveBitBinaryArray)) {
378+
$base32 .= $map[base_convert(str_pad($fiveBitBinaryArray[$i], 5,'0'), 2, 10)];
379+
$i++;
380+
}
381+
if($padding && ($x = strlen($binaryString) % 40) != 0) {
382+
if($x == 8) $base32 .= str_repeat($map[32], 6);
383+
else if($x == 16) $base32 .= str_repeat($map[32], 4);
384+
else if($x == 24) $base32 .= str_repeat($map[32], 3);
385+
else if($x == 32) $base32 .= $map[32];
386+
}
387+
return $base32;
388+
}
389+
390+
function nsec3hash($qname, $salt=null, $iterations='-')
391+
{
392+
//echo $this->nsec3hash('www.lunetkade.nl', 'AB', 1);
393+
$salt = pack('H*' , $salt); //hex2bin() in php 5.4
394+
$iterations = intval($iterations);
395+
$toHash='';
396+
397+
$qparts = explode('.', strtolower($qname).'.');
398+
foreach ($qparts as $part)
399+
{
400+
$toHash .= chr(strlen($part)).$part;
401+
}
402+
403+
//echo bin2hex($toHash.$salt)."\n";
404+
do
405+
{
406+
$toHash .= $salt;
407+
$toHash = sha1($toHash, true);
408+
$iterations--;
409+
}
410+
while ($iterations >= 0);
411+
return base32encode($toHash);
412+
}
413+
414+
415+
348416
}

0 commit comments

Comments
 (0)