39 lines
731 B
Go
39 lines
731 B
Go
package email
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"hash"
|
|
"io"
|
|
"net/mail"
|
|
"os"
|
|
"sort"
|
|
|
|
"github.com/golang/glog"
|
|
)
|
|
|
|
// Hash will parse r as an email, and return a hash.Hash that has been applied
|
|
// to the values of the specified headers.
|
|
func Hash(r io.Reader, headers []string) (hash.Hash, error) {
|
|
// Add deterministic behavior regardless of the order the users specified.
|
|
sort.Strings(headers)
|
|
var name string
|
|
if f, ok := r.(*os.File); ok {
|
|
name = f.Name()
|
|
}
|
|
|
|
h := sha1.New()
|
|
msg, err := mail.ReadMessage(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, header := range headers {
|
|
v := msg.Header.Get(header)
|
|
if v == "" {
|
|
glog.V(2).Infoln(name, "Empty", header, "header")
|
|
}
|
|
io.WriteString(h, v)
|
|
}
|
|
return h, nil
|
|
}
|