Created
January 17, 2016 13:30
-
-
Save jtwaleson/b1c1d20cd40a8b4f72b5 to your computer and use it in GitHub Desktop.
Print checksums of certificates public key, entire certificate, subject field, issuer field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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