简体   繁体   中英

How to verify if a public key matched private key signature?

How does one verify that a public key matched private key?

On start of the application, 2048-bit RSA keys are loaded from base64 PEM encoded string. I wish to verify that the keys are valid and that they match before continuing. The signing and verification is done by the underlining library I'm using.

I could sign and verify dummy data, but I am looking for alternative solutions.

Starter playground: https://play.golang.org/p/tsB8Yp-xs47

The solution is pretty simple

func verifyKeyPair(private, public string) bool {
    // Handle errors here
    block, _ := pem.Decode([]byte(rsaPrivateKey))
    key, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
    pubBlock, _ := pem.Decode([]byte(rsaPublicKey))
    pubKey, _ := x509.ParsePKIXPublicKey(pubBlock.Bytes)
    return key.PublicKey.Equal(pubKey)
}

https://play.golang.org/p/tR6Ns0wDrlN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM