Skip to content

Commit

Permalink
add the support for ecdsa in hash certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlele committed Oct 30, 2024
1 parent 4663a1e commit e74bb2f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions utils/hash.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}

0 comments on commit e74bb2f

Please sign in to comment.