使用golang生成有效的dkim密钥

时间:2019-06-24 19:19:22

标签: go email-validation dkim

我正在尝试使用golang生成dkim密钥,我当前的代码是:

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/x509"
    "encoding/asn1"
    "encoding/gob"
    "encoding/pem"
    "fmt"
    "os"
)

func Keymaker(path string) {
    reader := rand.Reader
    bitSize := 2048

    key, err := rsa.GenerateKey(reader, bitSize)
    checkError(err)

    publicKey := key.PublicKey

    //saveGobKey("private.key", key)
    savePEMKey(path+".priv", key)

    //saveGobKey("public.key", publicKey)
    savePublicPEMKey(path+".pub", publicKey)
}

func savePEMKey(fileName string, key *rsa.PrivateKey) {
    outFile, err := os.Create(fileName)
    checkError(err)
    defer outFile.Close()

    var privateKey = &pem.Block{
        Type:  "PRIVATE KEY",
        Bytes: x509.MarshalPKCS1PrivateKey(key),
    }

    err = pem.Encode(outFile, privateKey)
    checkError(err)
}

func savePublicPEMKey(fileName string, pubkey rsa.PublicKey) {
    asn1Bytes, err := asn1.Marshal(pubkey)
    checkError(err)

    var pemkey = &pem.Block{
        Type:  "PUBLIC KEY",
        Bytes: asn1Bytes,
    }

    pemfile, err := os.Create(fileName)
    checkError(err)
    defer pemfile.Close()

    err = pem.Encode(pemfile, pemkey)
    checkError(err)
}

func checkError(err error) {
    if err != nil {
        fmt.Println("Fatal error ", err.Error())
        os.Exit(1)
    }
}

代码正在运行,生成dkim私钥/公钥,问题是如果我使用此站点生成密钥,则此密钥会由发件人返回不良密钥:https://easydmarc.com/tools/dkim-record-generator/密钥结果有效。

这是我在dns中的dkim设置:

v=DKIM1;t=s;p=MIGJAoGBD1DSPRFFTujiCAiadP9ZeA6eAyf0GktfCQO2WKbrAx2Cu15y2A+agQMWNTmZbbhcznGZiz3kA9y7c0wOqN2iaJchBAxt4qdg1vUhy+6CkSZcY5bdm8uOxsXW3gjOTyZrOur/c118wMTOmSJKHOjn9xLG3ta8tXJtqab8idtx4TlBAgMBAAE=

我尝试低估easydmarc.com生成的密钥正常工作的方式,而golang密钥无效。有什么想法吗?

0 个答案:

没有答案