github.com/haraldrudell/parl@v0.4.176/parlca/ed25519-private_test.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/tls"
    12  	"crypto/x509"
    13  	"testing"
    14  
    15  	"github.com/haraldrudell/parl"
    16  )
    17  
    18  func TestNewEd25519(t *testing.T) {
    19  	var ed25519PrivateKey ed25519.PrivateKey // type: []byte
    20  	// implements crypto.Signer: Public, Sign
    21  	_ = ed25519PrivateKey.Equal  // func (ed25519.PrivateKey).Equal(x crypto.PrivateKey) bool
    22  	_ = ed25519PrivateKey.Public // func (ed25519.PrivateKey).Public() crypto.PublicKey
    23  	_ = ed25519PrivateKey.Seed   // func (ed25519.PrivateKey).Seed() []byte
    24  	_ = ed25519PrivateKey.Sign   // func (ed25519.PrivateKey).Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error)
    25  	_ = ed25519PrivateKey
    26  
    27  	var cryptoPrivateKey crypto.PrivateKey // interface{} no methods
    28  	_ = cryptoPrivateKey
    29  
    30  	var cryptoPublicKey crypto.PublicKey // interface{} no methods
    31  	_ = cryptoPublicKey
    32  
    33  	var cryptoSigner ed25519.PrivateKey // interface, ed25519.PrivateKey implements crypto.Signer
    34  	_ = cryptoSigner.Public             // func (crypto.Signer).Public() crypto.PublicKey
    35  	_ = cryptoSigner.Sign               // func (crypto.Signer).Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error)
    36  	_ = cryptoSigner
    37  
    38  	var tlsCertiticate tls.Certificate
    39  	_ = tlsCertiticate.PrivateKey // crypto.PrivateKey
    40  	// comment on field PrivateKey crypto.PrivateKey: This must implement crypto.Signer with an RSA, ECDSA or Ed25519 PublicKey
    41  
    42  	var _ x509.PublicKeyAlgorithm // int
    43  
    44  	var ed25519PublicKey ed25519.PublicKey // []byte
    45  	_ = ed25519PublicKey.Equal             // func (ed25519.PublicKey).Equal(x crypto.PublicKey) bool
    46  	_ = ed25519PublicKey
    47  
    48  	keyPair, err := NewEd25519()
    49  	if err != nil {
    50  		t.Error(err)
    51  		return
    52  	}
    53  
    54  	if keyPair == nil {
    55  		t.Errorf("NewEd25519 returned nil")
    56  		return
    57  	}
    58  	/*
    59  		if !keyPair.HasKey() {
    60  			t.Error(perrors.New("keyPair empty"))
    61  			return
    62  		}
    63  	*/
    64  	algo := keyPair.Algo()
    65  	if algo != x509.Ed25519 {
    66  		t.Errorf("Unknown algo: %s", algo.String())
    67  		return
    68  	}
    69  	var keyDER parl.PrivateKeyDer
    70  	if keyDER, err = keyPair.DER(); err != nil {
    71  		t.Error(err)
    72  		return
    73  	}
    74  	if len(keyDER) == 0 {
    75  		t.Errorf("private key empty")
    76  		return
    77  	}
    78  	publicKey := keyPair.PublicKey()
    79  
    80  	if len(publicKey.DERe()) == 0 {
    81  		t.Errorf("public key empty")
    82  		return
    83  	}
    84  
    85  	var key Ed25519PrivateKey
    86  	if err = key.Validate(); err == nil {
    87  		t.Errorf("Mising expected error: Ed25519PrivateKey.Validate")
    88  	}
    89  }