Skip to content

Instantly share code, notes, and snippets.

@vbatts
Last active September 27, 2016 16:39
Show Gist options
  • Save vbatts/ea6df817924e7ade5d60bba163d51867 to your computer and use it in GitHub Desktop.
Save vbatts/ea6df817924e7ade5d60bba163d51867 to your computer and use it in GitHub Desktop.
get the digest of the public key per rfc5280 https://tools.ietf.org/html/rfc5280#section-4.2.1.2
package main
import (
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"encoding/asn1"
"fmt"
"log"
"os"
)
// Usage: go run keyid.go ./my.crt ./my.key
// > f1f6a4a0cd859c01a5458d8bc39190a6435793bd
func main() {
cert, err := tls.LoadX509KeyPair(os.Args[1], os.Args[2])
if err != nil {
log.Fatal(err)
}
key, ok := cert.PrivateKey.(*rsa.PrivateKey)
if !ok {
log.Fatal("does not look like an RSA key")
}
mdata, err := asn1.Marshal(key.PublicKey)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%x\n", sha1.Sum(mdata))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment