github.com/MetalBlockchain/metalgo@v1.11.9/staking/tls_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package staking
     5  
     6  import (
     7  	"crypto"
     8  	"crypto/rand"
     9  	"fmt"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/MetalBlockchain/metalgo/utils/hashing"
    16  )
    17  
    18  func TestMakeKeys(t *testing.T) {
    19  	require := require.New(t)
    20  
    21  	cert, err := NewTLSCert()
    22  	require.NoError(err)
    23  
    24  	msg := []byte(fmt.Sprintf("msg %d", time.Now().Unix()))
    25  	msgHash := hashing.ComputeHash256(msg)
    26  
    27  	sig, err := cert.PrivateKey.(crypto.Signer).Sign(rand.Reader, msgHash, crypto.SHA256)
    28  	require.NoError(err)
    29  
    30  	require.NoError(cert.Leaf.CheckSignature(cert.Leaf.SignatureAlgorithm, msg, sig))
    31  }
    32  
    33  func BenchmarkNewCertAndKeyBytes(b *testing.B) {
    34  	for i := 0; i < b.N; i++ {
    35  		_, _, err := NewCertAndKeyBytes()
    36  		require.NoError(b, err)
    37  	}
    38  }