gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/ecdsa_ext/ecdsa_ext_test.go (about) 1 // Copyright (c) 2023 zhaochun 2 // core-gm is licensed under Mulan PSL v2. 3 // You can use this software according to the terms and conditions of the Mulan PSL v2. 4 // You may obtain a copy of Mulan PSL v2 at: 5 // http://license.coscl.org.cn/MulanPSL2 6 // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 7 // See the Mulan PSL v2 for more details. 8 9 package ecdsa_ext 10 11 import ( 12 "crypto/elliptic" 13 "crypto/rand" 14 "crypto/sha256" 15 "encoding/hex" 16 "fmt" 17 "testing" 18 ) 19 20 func TestPrivateKey_Sign(t *testing.T) { 21 22 privateKey, err := GenerateKey(elliptic.P256(), rand.Reader) 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 msg := "花有重开日, 人无再少年" 28 digest := sha256.Sum256([]byte(msg)) 29 fmt.Printf("msg: %s\n", msg) 30 fmt.Printf("digest hex: %s\n", hex.EncodeToString(digest[:])) 31 32 sign, err := privateKey.Sign(rand.Reader, digest[:], nil) 33 if err != nil { 34 t.Fatal(err) 35 } 36 fmt.Printf("sign hex: %s\n", hex.EncodeToString(sign)) 37 38 pubKey, ok := privateKey.Public().(*PublicKey) 39 if !ok { 40 t.Fatal("PublicKey类型强转失败") 41 } 42 valied, err := pubKey.EcVerify(digest[:], sign, nil) 43 if !valied { 44 t.Fatal("验签失败") 45 } 46 fmt.Println("验签成功") 47 }