github.com/haraldrudell/parl@v0.4.176/parlca/ed25519-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/ed25519" 10 "crypto/x509" 11 "encoding/pem" 12 13 "github.com/haraldrudell/parl" 14 "github.com/haraldrudell/parl/perrors" 15 ) 16 17 type Ed25519PublicKey struct { 18 ed25519.PublicKey // Equal() 19 } 20 21 var _ parl.PublicKey = &Ed25519PublicKey{} 22 23 func (key *Ed25519PublicKey) DER() (publicKeyDer parl.PublicKeyDer, err error) { 24 if len(key.PublicKey) == 0 { 25 err = perrors.New("Uninitialized ed25519 public key") 26 return 27 } else if len(key.PublicKey) != ed25519.PublicKeySize { 28 err = perrors.Errorf("Bad ed25519 public key length: %d exp: %d", len(key.PublicKey), ed25519.PublicKeySize) 29 return 30 } 31 var byts []byte 32 if byts, err = x509.MarshalPKIXPublicKey(key.PublicKey); perrors.IsPF(&err, "x509.MarshalPKIXPublicKey %w", err) { 33 return 34 } 35 publicKeyDer = byts 36 return 37 } 38 39 func (key *Ed25519PublicKey) DERe() (publicKeyDer parl.PublicKeyDer) { 40 var err error 41 if publicKeyDer, err = key.DER(); err != nil { 42 panic(err) 43 } 44 return 45 } 46 47 func (key *Ed25519PublicKey) PEM() (pemBytes parl.PemBytes, err error) { 48 block := pem.Block{ 49 Type: pemPublicKeyType, 50 } 51 if block.Bytes, err = key.DER(); err != nil { 52 return 53 } 54 pemBytes = append([]byte(PemText()), pem.EncodeToMemory(&block)...) 55 return 56 } 57 58 func (key *Ed25519PublicKey) PEMe() (pemBytes parl.PemBytes) { 59 var err error 60 if pemBytes, err = key.PEM(); err != nil { 61 panic(err) 62 } 63 return 64 } 65 66 func (key *Ed25519PublicKey) Algo() (algo x509.PublicKeyAlgorithm) { 67 return x509.Ed25519 68 }