github.com/turingchain2020/turingchain@v1.1.21/common/crypto/types.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package crypto
     6  
     7  //Crypto 加密
     8  type Crypto interface {
     9  	GenKey() (PrivKey, error)
    10  	SignatureFromBytes([]byte) (Signature, error)
    11  	PrivKeyFromBytes([]byte) (PrivKey, error)
    12  	PubKeyFromBytes([]byte) (PubKey, error)
    13  	Validate(msg, pub, sig []byte) error
    14  }
    15  
    16  //AggregateCrypto 聚合签名
    17  type AggregateCrypto interface {
    18  	Aggregate(sigs []Signature) (Signature, error)
    19  	AggregatePublic(pubs []PubKey) (PubKey, error)
    20  	VerifyAggregatedOne(pubs []PubKey, m []byte, sig Signature) error
    21  	VerifyAggregatedN(pubs []PubKey, ms [][]byte, sig Signature) error
    22  }
    23  
    24  //PrivKey 私钥
    25  type PrivKey interface {
    26  	Bytes() []byte
    27  	Sign(msg []byte) Signature
    28  	PubKey() PubKey
    29  	Equals(PrivKey) bool
    30  }
    31  
    32  //Signature 签名
    33  type Signature interface {
    34  	Bytes() []byte
    35  	IsZero() bool
    36  	String() string
    37  	Equals(Signature) bool
    38  }
    39  
    40  //PubKey 公钥
    41  type PubKey interface {
    42  	Bytes() []byte
    43  	KeyString() string
    44  	VerifyBytes(msg []byte, sig Signature) bool
    45  	Equals(PubKey) bool
    46  }
    47  
    48  //CertSignature 签名
    49  type CertSignature struct {
    50  	Signature []byte
    51  	Cert      []byte
    52  }
    53  
    54  // Config crypto模块配置
    55  type Config struct {
    56  	//支持只指定若干加密类型,不配置默认启用所有的加密插件, 如 types=["secp256k1", "sm2"]
    57  	EnableTypes []string `json:"enableTypes,omitempty"`
    58  	//支持对EnableTypes的每个加密插件分别设置启用高度, 不配置采用内置的启用高度
    59  	// [crypto.enableHeight]
    60  	// secp256k1=0
    61  	EnableHeight map[string]int64 `json:"enableHeight,omitempty"`
    62  }
    63  
    64  // DriverInitFunc 插件初始化接口,参数是序列化的json数据,需要unmarshal为自定义的结构
    65  type DriverInitFunc func(jsonCfg []byte)
    66  
    67  // Driver 加密插件及相关信息
    68  type Driver struct {
    69  	crypto       Crypto
    70  	initFunc     DriverInitFunc
    71  	isCGO        bool  // 是否为cgo编译
    72  	enableHeight int64 // 启用高度
    73  	typeID       int32 //类型值
    74  }
    75  
    76  // Option 注册Driver时可选参数,设置相关参数默认值
    77  type Option func(*Driver) error