This commit is contained in:
Stéphane Depierrepont aka Toorop
2015-05-14 10:27:40 +02:00
parent 016293cce9
commit 773c7cd060
4 changed files with 54 additions and 17 deletions

View File

@@ -1,7 +1,47 @@
# go-dkim
DKIM package for golang
DKIM package for Golang
## RFCs
[5585](http://dkim.org/specs/rfc5585.html)
[5617](http://www.rfc-editor.org/rfc/rfc5617.txt)
[6376](http://tools.ietf.org/html/rfc6376)
## Getting started
### Install
```
go get https://github.com/toorop/go-dkim
```
### Sign email
```go
import (
dkim "github.com/toorop/go-dkim"
)
func main(){
// email is the email to sign (byte slice)
// privateKey the private key (pem encoded, byte slice )
options := dkim.NewSigOptions()
options.PrivateKey = privateKey
options.Domain = "mydomain.tld"
options.Selector = "myselector"
options.SignatureExpireIn = 3600
options.BodyLength = 50
options.Headers = []string{"from", "date", "mime-version", "received", "received"}
options.AddSignatureTimestamp = true
options.Canonicalization = "relaxed/relaxed"
err := dkim.Sign(&email, options)
// handle err..
// And... that's it, 'email' is now signed ! Amazing© !!!
}
```
### Verify
```go
import (
dkim "github.com/toorop/go-dkim"
)
func main(){
// email is the email to verify (byte slice)
status, err := Verify(&email)
// handle status, err
}
```

View File

@@ -43,7 +43,7 @@ type sigOptions struct {
Version uint
// Private key used for signing (required)
PrivateKey string
PrivateKey []byte
// Domain (required)
Domain string
@@ -101,10 +101,10 @@ func Sign(email *[]byte, options sigOptions) error {
var privateKey *rsa.PrivateKey
// PrivateKey
if options.PrivateKey == "" {
if len(options.PrivateKey) == 0 {
return ErrSignPrivateKeyRequired
}
d, _ := pem.Decode([]byte(options.PrivateKey))
d, _ := pem.Decode(options.PrivateKey)
key, err := x509.ParsePKCS1PrivateKey(d.Bytes)
if err != nil {
return ErrCandNotParsePrivateKey

View File

@@ -2,7 +2,6 @@ package dkim
import (
"bytes"
//"errors"
"fmt"
"net/mail"
"net/textproto"

View File

@@ -151,7 +151,7 @@ func Test_NewSigOptions(t *testing.T) {
assert.Equal(t, "simple/simple", options.Canonicalization)
}
/*func Test_SignConfig(t *testing.T) {
func Test_SignConfig(t *testing.T) {
email := []byte(emailBase)
emailToTest := append([]byte(nil), email...)
options := NewSigOptions()
@@ -159,7 +159,7 @@ func Test_NewSigOptions(t *testing.T) {
assert.NotNil(t, err)
// && err No private key
assert.EqualError(t, err, ErrSignPrivateKeyRequired.Error())
options.PrivateKey = privKey
options.PrivateKey = []byte(privKey)
emailToTest = append([]byte(nil), email...)
err = Sign(&emailToTest, options)
@@ -217,7 +217,7 @@ func Test_canonicalize(t *testing.T) {
options.Headers = []string{"from", "date", "mime-version", "received", "received", "In-Reply-To"}
// simple/simple
options.Canonicalization = "simple/simple"
header, body, err := canonicalize(&emailToTest, options)
header, body, err := canonicalize(&emailToTest, options.Canonicalization, options.Headers)
assert.NoError(t, err)
assert.Equal(t, []byte(headerSimple), header)
assert.Equal(t, []byte(bodySimple), body)
@@ -225,18 +225,18 @@ func Test_canonicalize(t *testing.T) {
// relaxed/relaxed
emailToTest = append([]byte(nil), email...)
options.Canonicalization = "relaxed/relaxed"
header, body, err = canonicalize(&emailToTest, options)
header, body, err = canonicalize(&emailToTest, options.Canonicalization, options.Headers)
assert.NoError(t, err)
assert.Equal(t, []byte(headerRelaxed), header)
assert.Equal(t, []byte(bodyRelaxed), body)
}
*/
func Test_Sign(t *testing.T) {
email := []byte(emailBase)
emailRelaxed := append([]byte(nil), email...)
options := NewSigOptions()
options.PrivateKey = privKey
options.PrivateKey = []byte(privKey)
options.Domain = domain
options.Selector = selector
//options.SignatureExpireIn = 3600
@@ -252,8 +252,6 @@ func Test_Sign(t *testing.T) {
emailSimple := append([]byte(nil), email...)
err = Sign(&emailSimple, options)
assert.Equal(t, []byte(signedSimpleSimple), emailSimple)
//fmt.Println(signedSimpleSimple)
//fmt.Println(string(emailSimple))
}