Debian Codegraph @

dnsutil/nsec3.go

raw
1 package dnsutil
2
3 import (
4 "crypto/sha1"
5 "encoding/base32"
6 "encoding/hex"
7
8 "codeberg.org/miekg/dns/internal/pack"
9 )
10
11 // NSEC3Name returns the hashed owner name according to RFC 5155. The hash is always SHA-1, the only algorithm
12 // RFC 5155 defines; when hashing a name for comparison with an NSEC3 record, check that record's Hash field
13 // first.
14 func NSEC3Name(s, salt string, iter uint16) string {
15 hashdata := make([]byte, hex.DecodedLen(len(salt))+255)
16 n, err := pack.Name(s, hashdata, 0, nil, false)
17 if err != nil {
18 return ""
19 }
20 m, err := pack.StringHex(salt, hashdata[n:], 0)
21 if err != nil {
22 return ""
23 }
24 hashdata = hashdata[:n+m]
25
26 hash := sha1.New()
27 // k = 0
28 hash.Write(hashdata)
29 nsec3 := hash.Sum(nil)
30
31 for range iter {
32 hash.Reset()
33 hash.Write(nsec3)
34 hash.Write(hashdata[n:])
35 nsec3 = hash.Sum(nil)
36 }
37 return base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(nsec3)
38 }