1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
package cryptoutil
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"fmt"
)
func PublicKeyEqual(a, b crypto.PublicKey) (bool, error) {
switch a := a.(type) {
case *rsa.PublicKey:
rsaPublicKey, ok := b.(*rsa.PublicKey)
return ok && RSAPublicKeyEqual(a, rsaPublicKey), nil
case *ecdsa.PublicKey:
ecdsaPublicKey, ok := b.(*ecdsa.PublicKey)
return ok && ECDSAPublicKeyEqual(a, ecdsaPublicKey), nil
case ed25519.PublicKey:
ed25519PublicKey, ok := b.(ed25519.PublicKey)
return ok && bytes.Equal(a, ed25519PublicKey), nil
default:
return false, fmt.Errorf("unsupported public key type %T", a)
}
}
func RSAPublicKeyEqual(a, b *rsa.PublicKey) bool {
return a.E == b.E && a.N.Cmp(b.N) == 0
}
func ECDSAPublicKeyEqual(a, b *ecdsa.PublicKey) bool {
return a.Curve == b.Curve && a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
}
|