gpg public 密钥验证

gpg public key validation

我需要验证来自用户输入的 gpg public 密钥,但 pem.Decode() 不适用于以下 public 密钥,除非我删除校验和 =dMc+结束。处理这个问题的正确方法是什么?谢谢

package main

import (
    "fmt"
    "encoding/pem"
)

func main() {
    pubkey :=`
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBGBNUNsBEADBys9wB8AKJRT02wgmcSsH8CmjyB7WOEnQn6OCjMvjdpGqCyko
iUYPWfx9mCJwJYHBJ9SoX4wHCmWXqmsAoxsUq5GgxE/+450GXjQh9Rh2pQdN0kEk
153FQGtEdouVls9B3B+b6F84TzF7E9RwjxK36/7iECJ7AA6h1Ww8KLMt9TUmQn/Z
fD2MvNXwyAT1gn/7M49qW+kBc/kh5rA8W9dZPbHIaiWkDDMo1qtZMt3XvdJa+Hux
tFvFoT8cjdv9JV+00iKpgTX2b1+QzL5Qx3TaaR4iFgAAzEaJIfatzlloVPLeC37T
DgTolQLDbWtWdIUvFYoVpx4NilwwAssHIeOgWA5/UGViJtpjKtQfwL0WnkJ+IqLt
2pvtghM0oDO5ip0gP6hQgk3mUQPZcfBYDZjeOmAT+9PwYOFe1r31V9PX+XFxGpoD
zXmphgwZl/9qJskHw5USQLGoyzgn0Fg8vqXG5AK5dxEIQEHhkZ5xD7zFxmmmU+y6
OQvbvpdV47b1+XfUQV0GKLW/cb5FZkBWn6j6P7vXC47gYpPu+S2o4FxcoBp9MtrA
ynfrh7+vyM7nw4DUAlP4+Tva4/WNikwbhMSKA2LnjgnJps5bemwdqEJSDDCTPLPX
hUsHDav/P0/W72ZxuEq0JYBskZXSjymtXLsLzmLMV09mhppglQcOVvDDdQARAQAB
tB50ZXN0bWFuIDx0ZXN0bWFuQHRlc3R0ZXN0LmNvbT6JAlQEEwEIAD4WIQRBWFz2
+2k8a+D58hHKpOJHohNm0AUCYE1Q2wIbAwUJB4YfZQULCQgHAgYVCgkICwIEFgID
AQIeAQIXgAAKCRDKpOJHohNm0J+BD/9HY59ivHEXhgXCMVb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BQu7
pa0EbEfq
=dMc+
-----END PGP PUBLIC KEY BLOCK-----`

    block, _ := pem.Decode([]byte(pubkey))

    if block != nil {
        fmt.Println("Decode worked : ", block.Type)
    }
}

您可以使用 golang.org/x/crypto/openpgp 包:

main.go

package main

import (
        "bytes"
        "fmt"
        "os"
        "golang.org/x/crypto/openpgp"
)

func main() {
        r := bytes.NewReader([]byte(pubKey))
        keys, err := openpgp.ReadArmoredKeyRing(r)
        if err != nil {
                fmt.Printf("Unable to read armored key ring: %v\n", err)
                os.Exit(1)
        }
        for i, key := range keys {
                fmt.Printf("Key #%d identities:\n", i + 1)
                for name, _ := range key.Identities {
                        fmt.Printf("\t%s\n", name)
                }
        }
}

const pubKey = `
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBGBNUNsBEADBys9wB8AKJRT02wgmcSsH8CmjyB7WOEnQn6OCjMvjdpGqCyko
iUYPWfx9mCJwJYHBJ9SoX4wHCmWXqmsAoxsUq5GgxE/+450GXjQh9Rh2pQdN0kEk
153FQGtEdouVls9B3B+b6F84TzF7E9RwjxK36/7iECJ7AA6h1Ww8KLMt9TUmQn/Z
fD2MvNXwyAT1gn/7M49qW+kBc/kh5rA8W9dZPbHIaiWkDDMo1qtZMt3XvdJa+Hux
tFvFoT8cjdv9JV+00iKpgTX2b1+QzL5Qx3TaaR4iFgAAzEaJIfatzlloVPLeC37T
DgTolQLDbWtWdIUvFYoVpx4NilwwAssHIeOgWA5/UGViJtpjKtQfwL0WnkJ+IqLt
2pvtghM0oDO5ip0gP6hQgk3mUQPZcfBYDZjeOmAT+9PwYOFe1r31V9PX+XFxGpoD
zXmphgwZl/9qJskHw5USQLGoyzgn0Fg8vqXG5AK5dxEIQEHhkZ5xD7zFxmmmU+y6
OQvbvpdV47b1+XfUQV0GKLW/cb5FZkBWn6j6P7vXC47gYpPu+S2o4FxcoBp9MtrA
ynfrh7+vyM7nw4DUAlP4+Tva4/WNikwbhMSKA2LnjgnJps5bemwdqEJSDDCTPLPX
hUs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T1qN7zqi9VU7uGMXCEsSZUbUotbIrmc4ckuGELq9ps8+c0uA05
P4DyLh5cKUIA7P7kpbYL52LCHwWr9XlxWs3ZtlWzsiX2BqsQhqr6VDO+A27BCj6Z
zfx4rra2qx63P3vCO6pmZRc625KNTHv59b5OOpMfTuvkuw7OuMQwql4n7lZLss6U
sDjoSlKEyuz2LXvP4PYrWQUp9xbB+TnKIyIBWie4yEAefsmaHbA0lXd5CplFBQu7
pa0EbEfq
=dMc+
-----END PGP PUBLIC KEY BLOCK-----`

Output

$ go run main.go
Key #1 identities:
        testman <testman@testtest.com>