ssl - 如何显示网站证书的公钥

标签 ssl go ssl-certificate

我创建了一个 Go 程序来连接到一个网站并获取它使用的证书。我不确定如何获得公钥的正确表示。

我可以获取证书,并且可以在 Certificate.PublicKey 上键入检查。一旦我理解它是 rsa.PublicKey 或 ecdsa.PublicKey,我就需要打印它的十六进制表示。

switch cert.PublicKey.(type) {
case *rsa.PublicKey:
    logrus.Error("this is RSA")
    // TODO: print hex representation of key
case *ecdsa.PublicKey:
    logrus.Error("this is ECDSA")
    // TODO: print hex representation of key
default:
    fmt.Println("it's something else")
}

我希望它打印出如下内容:

04 4B F9 47 1B A8 A8 CB A4 C6 C0 2D 45 DE 43 F3 BC F5 D2 98 F4 25 90 6F 13 0D 78 1A AC 05 B4 DF 7B F6 06 5C 80 97 9A 53 06 D0 DB 0E 15 AD 03 DE 14 09 D3 77 54 B1 4E 15 A8 AF E3 FD DC 9D AD E0 C5

最佳答案

您似乎在要求所涉及证书的 sha1 总和。 这是一个工作示例,它要求主机:端口并打印所涉及证书的总和

package main

import (
        "crypto/sha1"
        "crypto/tls"
        "fmt"
        "log"
        "os"
)

func main() {
        if len(os.Args) != 2 {
                log.Panic("call with argument of host:port")
        }
        log.SetFlags(log.Lshortfile)

        conf := &tls.Config{
                //InsecureSkipVerify: true,
        }
        fmt.Printf("dialing:%s\n", os.Args[1])
        conn, err := tls.Dial("tcp", os.Args[1], conf)
        if err != nil {
                log.Println(err)
                return
        }
        defer conn.Close()
        for i, v := range conn.ConnectionState().PeerCertificates {
                //edit: use %X for uppercase hex printing
                fmt.Printf("cert %d sha1 fingerprint:%x \n", i, sha1.Sum(v.Raw))
        }
}

运行方式:

./golang-tls www.google.com:443
dialing:www.google.com:443
cert 0 sha1 fingerprint:34781c3be98cf958f514aecb1ae2e4e866effe34
cert 1 sha1 fingerprint:eeacbd0cb452819577911e1e6203db262f84a318

关于 SSL 的一般概念,我找到了 this stackexchange answer非常有值(value)。

关于ssl - 如何显示网站证书的公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54384128/

相关文章:

php - 切换到 SSL 后登录 PHP session

android - 如何在 Square MockWebServer 中使用 SSL?

go - 如何将 Go 中间件模式与错误返回请求处理程序结合起来?

http - 是否可以使用 HTTP(而非 HTTPS)获得客户端证书?

python-3.x - 忽略或解决机器人框架中测试自动化的证书警告

apache - 如何删除安装在 Ubuntu apache2 服务器上的 letsencrypt ssl

apache - 使用 .htaccess 强制 SSL 的问题

web-services - 解释 : The underlying connection was closed: The connection was closed unexpectedly

concurrency - 并发 slice 访问

go - 在 Go 中解析包含结构的 yaml 对象