Add received path to hash, and has over repeated fields.

This commit is contained in:
Bill Thiede 2016-10-10 20:58:20 -07:00
parent 1699182671
commit 069ea6962a

23
hash.go
View File

@ -5,10 +5,9 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
"net/http"
"net/mail" "net/mail"
"sort" "sort"
"github.com/golang/glog"
) )
// Hasher is a list of headers that should be considered when hashing an email // Hasher is a list of headers that should be considered when hashing an email
@ -17,18 +16,20 @@ type Hasher []string
// Hash will parse r as an email, and return a hash.Hash that has been applied // Hash will parse r as an email, and return a hash.Hash that has been applied
// to the values of the headers in h. // to the values of the headers in h.
func (h Hasher) HashMessage(msg *mail.Message) (hash.Hash, error) { func (hsr Hasher) HashMessage(msg *mail.Message) (hash.Hash, error) {
// Add deterministic behavior regardless of the order the users specified. // Add deterministic behavior regardless of the order the users specified.
if !sort.IsSorted(sort.StringSlice(h)) { if !sort.IsSorted(sort.StringSlice(hsr)) {
sort.Strings(h) sort.Strings(hsr)
} }
hsh := sha1.New() hsh := sha1.New()
for _, header := range h { for _, h := range hsr {
v := msg.Header.Get(header) h := http.CanonicalHeaderKey(h)
if v == "" { vs, ok := msg.Header[h]
glog.V(2).Infoln("Empty", header, "header") if ok {
for _, v := range vs {
io.WriteString(hsh, v)
}
} }
io.WriteString(hsh, v)
} }
return hsh, nil return hsh, nil
} }
@ -41,7 +42,7 @@ func (h Hasher) HashReader(r io.Reader) (hash.Hash, error) {
return h.HashMessage(msg) return h.HashMessage(msg)
} }
var std = Hasher([]string{"to", "from", "cc", "date", "subject", "message-id"}) var std = Hasher([]string{"to", "from", "cc", "date", "subject", "message-id", "received"})
// Hash will parse r as an email, and return the hash as a hexadecimal string // Hash will parse r as an email, and return the hash as a hexadecimal string
// using a default set of headers. // using a default set of headers.