github.com/haraldrudell/parl@v0.4.176/parlca/ed25519-private.go (about) 1 /* 2 © 2021–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" 10 "crypto/ed25519" 11 "crypto/x509" 12 "encoding/pem" 13 14 "github.com/haraldrudell/parl" 15 "github.com/haraldrudell/parl/perrors" 16 ) 17 18 // Ed25519 implements parl.KeyPair for the x509.Ed25519 algorithm. 19 type Ed25519PrivateKey struct { 20 // func (ed25519.PrivateKey).Equal(x crypto.PrivateKey) bool 21 // func (ed25519.PrivateKey).Public() crypto.PublicKey 22 // func (ed25519.PrivateKey).Seed() []byte 23 // func (ed25519.PrivateKey).Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) 24 // implements crypto.Signer: Public, Sign 25 ed25519.PrivateKey // type: []byte 26 } 27 28 var _ crypto.Signer = &ed25519.PrivateKey{} 29 30 func NewEd25519() (privateKey parl.PrivateKey, err error) { 31 k := Ed25519PrivateKey{} 32 if _, k.PrivateKey, err = ed25519.GenerateKey(nil); perrors.IsPF(&err, "ed25519.GenerateKey %w", err) { 33 return 34 } 35 privateKey = &k 36 return 37 } 38 39 func (key *Ed25519PrivateKey) Algo() (algo x509.PublicKeyAlgorithm) { 40 return x509.Ed25519 41 } 42 43 func (key *Ed25519PrivateKey) DER() (privateKeyDer parl.PrivateKeyDer, err error) { 44 if privateKeyDer, err = x509.MarshalPKCS8PrivateKey(key.PrivateKey); err != nil { 45 err = perrors.Errorf("x509.MarshalPKCS8PrivateKey: '%w'", err) 46 } 47 return 48 } 49 50 func (key *Ed25519PrivateKey) DERe() (privateKeyDer parl.PrivateKeyDer) { 51 var err error 52 if privateKeyDer, err = key.DER(); err != nil { 53 panic(err) 54 } 55 return 56 } 57 58 /* 59 func (key *Ed25519PrivateKey) HasKey() (hasKey bool) { 60 return len(key.PrivateKey) > 0 61 } 62 63 func (key *Ed25519PrivateKey) HasPublicKey() (hasPublicKey bool) { 64 return len(key.PrivateKey) > 0 65 } 66 67 func (key *Ed25519PrivateKey) Public() (publicKey parl.PublicKey) { 68 publicKey = &Ed25519Public{PublicKey: key.PrivateKey.Public().(ed25519.PublicKey)} 69 return 70 } 71 72 func (key *Ed25519PrivateKey) PrivateBytes() (bytes []byte) { 73 return key.Seed() 74 } 75 */ 76 77 func (key *Ed25519PrivateKey) PEM() (pemBytes parl.PemBytes, err error) { 78 block := pem.Block{ 79 Type: pemPrivateKeyType, 80 } 81 if block.Bytes, err = key.DER(); err != nil { 82 return 83 } 84 pemBytes = append([]byte(PemText(key.PrivateKey.Seed())), pem.EncodeToMemory(&block)...) 85 return 86 } 87 88 func (key *Ed25519PrivateKey) PEMe() (pemBytes parl.PemBytes) { 89 var err error 90 if pemBytes, err = key.PEM(); err != nil { 91 panic(err) 92 } 93 return 94 } 95 96 func (key *Ed25519PrivateKey) PublicKey() (publicKey parl.PublicKey) { 97 return &Ed25519PublicKey{PublicKey: key.PrivateKey.Public().(ed25519.PublicKey)} 98 } 99 100 func (key *Ed25519PrivateKey) Validate() (err error) { 101 length := len(key.PrivateKey) 102 if length == 0 { 103 return perrors.New("ed25519 private key uninitialized") 104 } 105 if length != ed25519.PrivateKeySize { 106 return perrors.New("ed25519 private key corrupt") 107 } 108 return 109 }