github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/commons/signatures/ecdsa_test.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package signatures_test
    19  
    20  import (
    21  	"crypto/ecdsa"
    22  	"crypto/elliptic"
    23  	"crypto/rand"
    24  	"crypto/x509"
    25  	"encoding/pem"
    26  	"fmt"
    27  	"github.com/aacfactory/fns/commons/signatures"
    28  	"testing"
    29  	"time"
    30  )
    31  
    32  func TestECDSA(t *testing.T) {
    33  	key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
    34  	der, _ := x509.MarshalPKCS8PrivateKey(key)
    35  	keyPEM := pem.EncodeToMemory(&pem.Block{
    36  		Type:    "PRIVATE KEY",
    37  		Headers: nil,
    38  		Bytes:   der,
    39  	})
    40  	s, sErr := signatures.ECDSA(keyPEM, signatures.XXHash)
    41  	if sErr != nil {
    42  		t.Errorf("%+v", sErr)
    43  		return
    44  	}
    45  	p := []byte(time.Now().String())
    46  	v := s.Sign(p)
    47  	fmt.Println(s.Verify(p, v))
    48  	fmt.Println(len(v))
    49  }