github.com/haraldrudell/parl@v0.4.176/parlca/ecdsa-public.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package parlca
     7  
     8  import (
     9  	"crypto/ecdsa"
    10  	"crypto/x509"
    11  	"encoding/pem"
    12  
    13  	"github.com/haraldrudell/parl"
    14  	"github.com/haraldrudell/parl/perrors"
    15  )
    16  
    17  type EcdsaPublicKey struct {
    18  	ecdsa.PublicKey
    19  }
    20  
    21  func (key *EcdsaPublicKey) Algo() (algo x509.PublicKeyAlgorithm) {
    22  	return x509.ECDSA
    23  }
    24  
    25  func (key *EcdsaPublicKey) DER() (publicKeyDer parl.PublicKeyDer, err error) {
    26  	publicKeyDer, err = x509.MarshalPKIXPublicKey(&key.PublicKey)
    27  	perrors.IsPF(&err, "x509.MarshalPKIXPublicKey %w", err)
    28  	return
    29  }
    30  
    31  func (key *EcdsaPublicKey) DERe() (publicKeyDer parl.PublicKeyDer) {
    32  	var err error
    33  	if publicKeyDer, err = key.DER(); err != nil {
    34  		panic(err)
    35  	}
    36  	return
    37  }
    38  
    39  func (key *EcdsaPublicKey) PEM() (pemBytes parl.PemBytes, err error) {
    40  	block := pem.Block{
    41  		Type: pemPublicKeyType,
    42  	}
    43  	if block.Bytes, err = key.DER(); err != nil {
    44  		return
    45  	}
    46  	pemBytes = append([]byte(PemText()), pem.EncodeToMemory(&block)...)
    47  	return
    48  }
    49  
    50  func (key *EcdsaPublicKey) PEMe() (pemBytes parl.PemBytes) {
    51  	var err error
    52  	if pemBytes, err = key.PEM(); err != nil {
    53  		panic(err)
    54  	}
    55  	return
    56  }