package dns
import (
"crypto/sha1"
"encoding/hex"
"strings"
)
func HashName (label string , ha uint8 , iter uint16 , salt string ) string {
if ha != SHA1 {
return ""
}
wireSalt := make ([]byte , hex .DecodedLen (len (salt )))
n , err := packStringHex (salt , wireSalt , 0 )
if err != nil {
return ""
}
wireSalt = wireSalt [:n ]
name := make ([]byte , 255 )
off , err := PackDomainName (strings .ToLower (label ), name , 0 , nil , false )
if err != nil {
return ""
}
name = name [:off ]
s := sha1 .New ()
s .Write (name )
s .Write (wireSalt )
nsec3 := s .Sum (nil )
for k := uint16 (0 ); k < iter ; k ++ {
s .Reset ()
s .Write (nsec3 )
s .Write (wireSalt )
nsec3 = s .Sum (nsec3 [:0 ])
}
return toBase32 (nsec3 )
}
func (rr *NSEC3 ) Cover (name string ) bool {
nameHash := HashName (name , rr .Hash , rr .Iterations , rr .Salt )
owner := strings .ToUpper (rr .Hdr .Name )
labelIndices := Split (owner )
if len (labelIndices ) < 2 {
return false
}
ownerHash := owner [:labelIndices [1 ]-1 ]
ownerZone := owner [labelIndices [1 ]:]
if !IsSubDomain (ownerZone , strings .ToUpper (name )) {
return false
}
nextHash := rr .NextDomain
if ownerHash == nextHash && nameHash != ownerHash {
return true
}
if ownerHash > nextHash {
if nameHash > ownerHash {
return true
}
return nameHash < nextHash
}
if nameHash < ownerHash {
return false
}
return nameHash < nextHash
}
func (rr *NSEC3 ) Match (name string ) bool {
nameHash := HashName (name , rr .Hash , rr .Iterations , rr .Salt )
owner := strings .ToUpper (rr .Hdr .Name )
labelIndices := Split (owner )
if len (labelIndices ) < 2 {
return false
}
ownerHash := owner [:labelIndices [1 ]-1 ]
ownerZone := owner [labelIndices [1 ]:]
if !IsSubDomain (ownerZone , strings .ToUpper (name )) {
return false
}
if ownerHash == nameHash {
return true
}
return false
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .