clean code
This commit is contained in:
8
dkim.go
8
dkim.go
@@ -11,12 +11,10 @@ import (
|
|||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
//"fmt"
|
|
||||||
"hash"
|
"hash"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
//"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -89,7 +87,6 @@ func NewSigOptions() sigOptions {
|
|||||||
// Sign signs an email
|
// Sign signs an email
|
||||||
func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) {
|
func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) {
|
||||||
var privateKey *rsa.PrivateKey
|
var privateKey *rsa.PrivateKey
|
||||||
// check && sanitize config
|
|
||||||
|
|
||||||
// PrivateKey (required & TODO: valid)
|
// PrivateKey (required & TODO: valid)
|
||||||
if options.PrivateKey == "" {
|
if options.PrivateKey == "" {
|
||||||
@@ -99,7 +96,7 @@ func Sign(email *bytes.Reader, options sigOptions) (*bytes.Reader, error) {
|
|||||||
d, _ := pem.Decode([]byte(options.PrivateKey))
|
d, _ := pem.Decode([]byte(options.PrivateKey))
|
||||||
key, err := x509.ParsePKCS1PrivateKey(d.Bytes)
|
key, err := x509.ParsePKCS1PrivateKey(d.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, ErrCandNotParsePrivateKey
|
||||||
}
|
}
|
||||||
privateKey = key
|
privateKey = key
|
||||||
|
|
||||||
@@ -237,8 +234,7 @@ func canonicalize(emailReader *bytes.Reader, options sigOptions) (headers, body
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//fmt.Println(email)
|
// TODO: \n -> \r\n
|
||||||
// todo \n -> \r\n
|
|
||||||
parts := bytes.SplitN(email, []byte{13, 10, 13, 10}, 2)
|
parts := bytes.SplitN(email, []byte{13, 10, 13, 10}, 2)
|
||||||
|
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
|
|||||||
@@ -206,7 +206,7 @@ func NewDkimHeaderBySigOptions(options sigOptions) *DkimHeader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetHeaderBase return base header for signers
|
// GetHeaderBase return base header for signers
|
||||||
// Todo: some refactoring...
|
// Todo: some refactoring needed...
|
||||||
func (d *DkimHeader) GetHeaderBase(bodyHash string) string {
|
func (d *DkimHeader) GetHeaderBase(bodyHash string) string {
|
||||||
h := "DKIM-Signature: v=" + d.Version + "; a=" + d.Algorithm + "; q=" + strings.Join(d.QueryMethods, ":") + "; c=" + d.MessageCanonicalization + ";" + CRLF + TAB
|
h := "DKIM-Signature: v=" + d.Version + "; a=" + d.Algorithm + "; q=" + strings.Join(d.QueryMethods, ":") + "; c=" + d.MessageCanonicalization + ";" + CRLF + TAB
|
||||||
subh := "s=" + d.Selector + ";"
|
subh := "s=" + d.Selector + ";"
|
||||||
@@ -298,5 +298,3 @@ func (d *DkimHeader) GetHeaderBase(bodyHash string) string {
|
|||||||
h += subh + ";" + FWS + "b="
|
h += subh + ";" + FWS + "b="
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
//'test._domainkey.tmail.io:v=DKIM1;k=rsa;s=email;h=sha256;t=y;p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNUXO+Qsl1tw+GjrqFajz0ERSEUs1FHSL/+udZRWn1Atw8gz0+tcGqhWChBDeU9gY5sKLEAZnX3FjC/T/IbqeiSM68kS5vLkzRI84eiJrm3+IieUqIIicsO+WYxQs+JgVx5XhpPjX4SQjHtwEC2xKkWnEv+VPgO1JWdooURcSC6QIDAQAB':300::
|
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ var (
|
|||||||
// If bad Canonicalization parameter
|
// If bad Canonicalization parameter
|
||||||
ErrSignBadCanonicalization = errors.New("bad Canonicalization parameter")
|
ErrSignBadCanonicalization = errors.New("bad Canonicalization parameter")
|
||||||
|
|
||||||
|
// when unable to parse private key
|
||||||
|
ErrCandNotParsePrivateKey = errors.New("can not parse private key, check format (pem) and validity")
|
||||||
|
|
||||||
// Bad algorithm
|
// Bad algorithm
|
||||||
ErrSignBadAlgo = errors.New("bad algorithm. Only rsa-sha1 or rsa-sha256 are permitted")
|
ErrSignBadAlgo = errors.New("bad algorithm. Only rsa-sha1 or rsa-sha256 are permitted")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user