簡體   English   中英

如何顯示網站證書的公鑰

[英]How to show the public key for a website certificate

我創建了一個Go程序來連接到網站並獲取其使用的證書。 我不確定如何獲得公鑰的正確表示。

我可以獲取證書,並且可以在Certificate.PublicKey上鍵入check。 一旦了解了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總和。 這是一個要求提供host:port並打印所涉及證書總和的工作示例

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的一般概念,我發現這個stackexchange答案非常有價值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM