1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
# go-msgauth
[](https://godocs.io/github.com/emersion/go-msgauth)
[](https://builds.sr.ht/~emersion/go-msgauth/commits/master)
A Go library to authenticate e-mails:
* Create and verify [DKIM signatures][DKIM]
* Create and parse [Authentication-Results header fields][Authentication-Results]
* Fetch [DMARC] records
## DKIM [](https://godocs.io/github.com/emersion/go-msgauth/dkim)
### Sign
```go
r := strings.NewReader(mailString)
options := &dkim.SignOptions{
Domain: "example.org",
Selector: "brisbane",
Signer: privateKey,
}
var b bytes.Buffer
if err := dkim.Sign(&b, r, options); err != nil {
log.Fatal(err)
}
```
### Verify
```go
r := strings.NewReader(mailString)
verifications, err := dkim.Verify(r)
if err != nil {
log.Fatal(err)
}
for _, v := range verifications {
if v.Err == nil {
log.Println("Valid signature for:", v.Domain)
} else {
log.Println("Invalid signature for:", v.Domain, v.Err)
}
}
```
### FAQ
**Why can't I verify a `mail.Message` directly?** A `mail.Message` header is
already parsed, and whitespace characters (especially continuation lines) are
removed. Thus, the signature computed from the parsed header is not the same as
the one computed from the raw header.
**How can I publish my public key?** You have to add a TXT record to your DNS
zone. See [RFC 6376 appendix C](https://tools.ietf.org/html/rfc6376#appendix-C).
## Authentication-Results [](https://godocs.io/github.com/emersion/go-msgauth/authres)
```go
// Format
results := []authres.Result{
&authres.SPFResult{Value: authres.ResultPass, From: "example.net"},
&authres.AuthResult{Value: authres.ResultPass, Auth: "sender@example.com"},
}
s := authres.Format("example.com", results)
log.Println(s)
// Parse
identifier, results, err := authres.Parse(s)
if err != nil {
log.Fatal(err)
}
log.Println(identifier, results)
```
## DMARC [](https://godocs.io/github.com/emersion/go-msgauth/dmarc)
See the GoDoc page.
## License
MIT
[DKIM]: https://tools.ietf.org/html/rfc6376
[Authentication-Results]: https://tools.ietf.org/html/rfc7601
[DMARC]: http://tools.ietf.org/html/rfc7489
|