diff --git a/utils/hash.go b/utils/hash.go index 7c3e011..c99b240 100644 --- a/utils/hash.go +++ b/utils/hash.go @@ -1,8 +1,10 @@ package utils import ( + "crypto/ecdsa" "crypto/rsa" "fmt" + "math/big" "github.com/iden3/go-iden3-crypto/keccak256" "github.com/rarimo/certificate-transparency-go/x509" @@ -14,12 +16,19 @@ const ignoredKeyLength = 768 var ErrUnsupportedPublicKey = errors.New("unsupported public key, supported formats: rsa, ecdsa") -// HashCertificate hashes the RSA public key of the certificate +// HashCertificate hashes the public key of the certificate func HashCertificate(certificate *x509.Certificate) ([]byte, error) { - rsaPK, ok := certificate.PublicKey.(*rsa.PublicKey) - if !ok { + var keyValue *big.Int + + switch key := certificate.PublicKey.(type) { + case *rsa.PublicKey: + keyValue = key.N + case *ecdsa.PublicKey: + rawKeyBytes := append(key.X.Bytes(), key.Y.Bytes()...) + keyValue = new(big.Int).SetBytes(rawKeyBytes) + default: return nil, fmt.Errorf("%T: %w", certificate.PublicKey, ErrUnsupportedPublicKey) } - return keccak256.Hash(rsaPK.N.Bytes()), nil + return keccak256.Hash(keyValue.Bytes()), nil }