Hoist SHA1 check in HashName
That's all that's supported by this function, and moving it makes the code cleaner.
This commit is contained in:
parent
c2889aea81
commit
71bbe52b67
16
nsecx.go
16
nsecx.go
|
@ -3,36 +3,35 @@ package dns
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"hash"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HashName hashes a string (label) according to RFC 5155. It returns the hashed string in uppercase.
|
// HashName hashes a string (label) according to RFC 5155. It returns the hashed string in uppercase.
|
||||||
func HashName(label string, ha uint8, iter uint16, salt string) string {
|
func HashName(label string, ha uint8, iter uint16, salt string) string {
|
||||||
|
if ha != SHA1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
wireSalt := make([]byte, hex.DecodedLen(len(salt)))
|
wireSalt := make([]byte, hex.DecodedLen(len(salt)))
|
||||||
n, err := packStringHex(salt, wireSalt, 0)
|
n, err := packStringHex(salt, wireSalt, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
wireSalt = wireSalt[:n]
|
wireSalt = wireSalt[:n]
|
||||||
|
|
||||||
name := make([]byte, 255)
|
name := make([]byte, 255)
|
||||||
off, err := PackDomainName(strings.ToLower(label), name, 0, nil, false)
|
off, err := PackDomainName(strings.ToLower(label), name, 0, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
name = name[:off]
|
name = name[:off]
|
||||||
var s hash.Hash
|
|
||||||
switch ha {
|
|
||||||
case SHA1:
|
|
||||||
s = sha1.New()
|
|
||||||
default:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
|
s := sha1.New()
|
||||||
// k = 0
|
// k = 0
|
||||||
s.Write(name)
|
s.Write(name)
|
||||||
s.Write(wireSalt)
|
s.Write(wireSalt)
|
||||||
nsec3 := s.Sum(nil)
|
nsec3 := s.Sum(nil)
|
||||||
|
|
||||||
// k > 0
|
// k > 0
|
||||||
for k := uint16(0); k < iter; k++ {
|
for k := uint16(0); k < iter; k++ {
|
||||||
s.Reset()
|
s.Reset()
|
||||||
|
@ -40,6 +39,7 @@ func HashName(label string, ha uint8, iter uint16, salt string) string {
|
||||||
s.Write(wireSalt)
|
s.Write(wireSalt)
|
||||||
nsec3 = s.Sum(nsec3[:0])
|
nsec3 = s.Sum(nsec3[:0])
|
||||||
}
|
}
|
||||||
|
|
||||||
return toBase32(nsec3)
|
return toBase32(nsec3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue