1 Star 0 Fork 0

艾鸥科技 / go-aiou

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
secp256k1.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
张卓 提交于 2020-05-10 14:55 . init
package backend
import (
"crypto/rand"
"fmt"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"gitee.com/aiou-official/go-aiou/ilog"
)
// Secp256k1 is the secp256k1 crypto algorithm
type Secp256k1 struct{}
// Sign will signature the message with seckey by secp256k1
func (b *Secp256k1) Sign(message []byte, seckey []byte) []byte {
sig, err := secp256k1.Sign(message, seckey)
if err != nil {
ilog.Errorf("Failed to sign, %v", err)
return nil
}
return sig[:64]
}
// Verify will verify the message with pubkey and sig by secp256k1
func (b *Secp256k1) Verify(message []byte, pubkey []byte, sig []byte) bool {
return secp256k1.VerifySignature(pubkey, message, sig)
}
// GetPubkey will get the public key of the secret key by secp256k1
func (b *Secp256k1) GetPubkey(seckey []byte) []byte {
x, y := secp256k1.S256().ScalarBaseMult(seckey)
return secp256k1.CompressPubkey(x, y)
}
// GenSeckey will generate the secret key by secp256k1
func (b *Secp256k1) GenSeckey() []byte {
seckey := make([]byte, 32)
_, err := rand.Read(seckey)
if err != nil {
ilog.Errorf("Failed to random seckey, %v", err)
return nil
}
return seckey
}
// CheckSeckey ...
func (b *Secp256k1) CheckSeckey(seckey []byte) error {
if len(seckey) != 32 {
return fmt.Errorf("seckey length error secp256k1 seckey length should not be %v", len(seckey))
}
return nil
}
1
https://gitee.com/aiou-official/go-aiou.git
git@gitee.com:aiou-official/go-aiou.git
aiou-official
go-aiou
go-aiou
376a44096468

搜索帮助