dns/nsec3.go

59 lines
1.2 KiB
Go
Raw Normal View History

2011-01-27 14:52:58 +00:00
package dns
2011-03-07 20:56:36 +00:00
import (
2011-03-07 21:47:20 +00:00
"io"
"hash"
"strings"
"crypto/sha1"
2011-03-07 20:56:36 +00:00
)
2011-03-07 21:47:20 +00:00
type saltWireFmt struct {
Salt string "size-hex"
}
2011-01-27 14:52:58 +00:00
2011-03-30 13:44:28 +00:00
// Hash a string/label according to RFC5155.
func hashName(label string, ha int, iterations int, salt string) string {
2011-03-07 21:47:20 +00:00
saltwire := new(saltWireFmt)
saltwire.Salt = salt
wire := make([]byte, DefaultMsgSize)
n, ok := packStruct(saltwire, wire, 0)
if !ok {
return ""
}
wire = wire[:n]
2011-03-09 13:27:41 +00:00
name := make([]byte, 255)
off, ok1 := packDomainName(strings.ToLower(label), name, 0)
2011-03-07 21:47:20 +00:00
if !ok1 {
return ""
}
2011-03-09 13:27:41 +00:00
name = name[:off]
2011-03-07 21:47:20 +00:00
var s hash.Hash
switch ha {
case HashSHA1:
s = sha1.New()
2011-03-24 08:24:49 +00:00
default:
return ""
2011-03-07 21:47:20 +00:00
}
// k = 0
2011-03-09 13:27:41 +00:00
name = append(name, wire...)
io.WriteString(s, string(name))
2011-03-07 21:47:20 +00:00
nsec3 := s.Sum()
2011-03-24 08:24:49 +00:00
// k > 0
for k := 0; k < iterations; k++ {
s.Reset()
nsec3 = append(nsec3, wire...)
io.WriteString(s, string(nsec3))
nsec3 = s.Sum()
}
2011-03-07 21:47:20 +00:00
return unpackBase32(nsec3)
2011-03-07 20:56:36 +00:00
}
2011-03-09 17:54:55 +00:00
2011-03-30 13:44:28 +00:00
// Hash the ownername and the next owner name in an NSEC3 record according
// to RFC 5155.
// Use the parameters from the NSEC3 itself.
2011-03-09 17:54:55 +00:00
func (nsec3 *RR_NSEC3) HashNames() {
2011-03-30 13:44:28 +00:00
nsec3.Header().Name = hashName(nsec3.Header().Name, int(nsec3.Hash), int(nsec3.Iterations), nsec3.Salt)
nsec3.NextDomain = hashName(nsec3.NextDomain, int(nsec3.Hash), int(nsec3.Iterations), nsec3.Salt)
2011-03-09 17:54:55 +00:00
}