github.com/haraldrudell/parl@v0.4.176/parlca/rsa-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/rsa"
    10  	"crypto/x509"
    11  	"encoding/pem"
    12  
    13  	"github.com/haraldrudell/parl"
    14  )
    15  
    16  type RsaPublicKey struct {
    17  	rsa.PublicKey
    18  }
    19  
    20  func (key *RsaPublicKey) Algo() (algo x509.PublicKeyAlgorithm) {
    21  	return x509.RSA
    22  }
    23  
    24  func (key *RsaPublicKey) DER() (publicKeyDer parl.PublicKeyDer, err error) {
    25  	publicKeyDer = x509.MarshalPKCS1PublicKey(&key.PublicKey)
    26  	return
    27  }
    28  
    29  func (key *RsaPublicKey) DERe() (publicKeyDer parl.PublicKeyDer) {
    30  	var err error
    31  	if publicKeyDer, err = key.DER(); err != nil {
    32  		panic(err)
    33  	}
    34  	return
    35  }
    36  
    37  func (key *RsaPublicKey) PEM() (pemBytes parl.PemBytes, err error) {
    38  	block := pem.Block{
    39  		Type: pemPublicKeyType,
    40  	}
    41  	if block.Bytes, err = key.DER(); err != nil {
    42  		return
    43  	}
    44  	pemBytes = append([]byte(PemText()), pem.EncodeToMemory(&block)...)
    45  	return
    46  }
    47  
    48  func (key *RsaPublicKey) PEMe() (pemBytes parl.PemBytes) {
    49  	var err error
    50  	if pemBytes, err = key.PEM(); err != nil {
    51  		panic(err)
    52  	}
    53  	return
    54  }