github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/grantae/certinfo/README.md (about)

     1  # Certificate Information for Go
     2  
     3  A golang tool for printing x509 TLS certificates in a format similar to OpenSSL.
     4  
     5  ## Installation
     6  
     7  ``` bash
     8  go get github.com/grantae/certinfo
     9  ```
    10  
    11  ## Usage
    12  
    13  ### Print a certificate from a website
    14  
    15  ``` go
    16  package main
    17  
    18  import (
    19    "crypto/tls"
    20    "fmt"
    21    "github.com/grantae/certinfo"
    22    "log"
    23  )
    24  
    25  func main() {
    26    // Connect to google.com
    27    cfg := tls.Config{}
    28    conn, err := tls.Dial("tcp", "google.com:443", &cfg)
    29    if err != nil {
    30      log.Fatalln("TLS connection failed: " + err.Error())
    31    }
    32    // Grab the last certificate in the chain
    33    certChain := conn.ConnectionState().PeerCertificates
    34    cert := certChain[len(certChain)-1]
    35  
    36    // Print the certificate
    37    result, err := certinfo.CertificateText(cert)
    38    if err != nil {
    39      log.Fatal(err)
    40    }
    41    fmt.Print(result)
    42  }
    43  ```
    44  
    45  ### Print a PEM-encoded certificate from a file
    46  
    47  ``` go
    48  package main
    49  
    50  import (
    51    "github.com/hellobchain/newcryptosm/x509"
    52    "encoding/pem"
    53    "fmt"
    54    "github.com/grantae/certinfo"
    55    "io/ioutil"
    56    "log"
    57  )
    58  
    59  func main() {
    60    // Read and parse the PEM certificate file
    61    pemData, err := ioutil.ReadFile("cert.pem")
    62    if err != nil {
    63      log.Fatal(err)
    64    }
    65    block, rest := pem.Decode([]byte(pemData))
    66    if block == nil || len(rest) > 0 {
    67      log.Fatal("Certificate decoding error")
    68    }
    69    cert, err := x509.ParseCertificate(block.Bytes)
    70    if err != nil {
    71      log.Fatal(err)
    72    }
    73  
    74    // Print the certificate
    75    result, err := certinfo.CertificateText(cert)
    76    if err != nil {
    77      log.Fatal(err)
    78    }
    79    fmt.Print(result)
    80  }
    81  ```
    82  
    83  ## Testing
    84  
    85  ``` bash
    86  go test github.com/grantae/certinfo
    87  ```
    88  
    89  This compares several PEM-encoded certificates with their expected outputs.
    90  
    91  ## License
    92  
    93  MIT -- see `LICENSE` for more information.
    94