Skip to content

Instantly share code, notes, and snippets.

@royge
Forked from jtwaleson/print-certs.go
Created January 18, 2016 05:00
Show Gist options
  • Save royge/50d2ea890e883feb1e78 to your computer and use it in GitHub Desktop.
Save royge/50d2ea890e883feb1e78 to your computer and use it in GitHub Desktop.
Print checksums of certificates public key, entire certificate, subject field, issuer field
package main
import "crypto/sha1"
import "crypto/x509"
import "fmt"
import "encoding/pem"
import "os"
import "time"
import "bufio"
import "strings"
func handleCert(pemdata []byte) {
asn1block, _ := pem.Decode(pemdata)
if asn1block == nil {
fmt.Fprintln(os.Stderr, "no valid pem data found")
return
}
cert, err := x509.ParseCertificate(asn1block.Bytes)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
hash := sha1.New()
hash.Write(cert.Raw)
pkeyhash := sha1.New()
pkeyhash.Write(cert.RawSubjectPublicKeyInfo)
subjecth := sha1.New()
subjecth.Write(cert.RawSubject)
issuerh := sha1.New()
issuerh.Write(cert.RawIssuer)
fmt.Println(fmt.Sprintf("%X %X %X %X", pkeyhash.Sum(nil), hash.Sum(nil), subjecth.Sum(nil), issuerh.Sum(nil)))
}
}
func handleCerts(queue chan []byte) {
for {
handleCert(<-queue)
}
}
func main () {
cs := make(chan []byte)
for i := 0; i < 3; i++ {
go handleCerts(cs)
}
scanner := bufio.NewScanner(os.Stdin)
fragment := ""
for scanner.Scan() {
line := scanner.Text()
fragment = fragment + "\n" + line
if strings.Contains(line, "END CERTIFICATE") {
cs <- []byte(fragment)
fragment = ""
}
}
time.Sleep(2 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment