github.com/turingchain2020/turingchain@v1.1.21/common/crypto/crypto.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  package crypto
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"sync"
    12  )
    13  
    14  var (
    15  	//ErrNotSupportAggr 不支持聚合签名
    16  	ErrNotSupportAggr = errors.New("AggregateCrypto not support")
    17  	//ErrSign 签名错误
    18  	ErrSign = errors.New("error signature")
    19  )
    20  
    21  var (
    22  	drivers     = make(map[string]*Driver)
    23  	driversType = make(map[int32]string)
    24  	driverMutex sync.Mutex
    25  )
    26  
    27  // Init init crypto
    28  func Init(cfg *Config, subCfg map[string][]byte) {
    29  
    30  	if cfg == nil {
    31  		return
    32  	}
    33  	driverMutex.Lock()
    34  	defer driverMutex.Unlock()
    35  
    36  	// 未指定时,所有插件采用代码中的开启配置
    37  	if len(cfg.EnableTypes) > 0 {
    38  		//配置中指定了插件类型,先屏蔽所有
    39  		for _, d := range drivers {
    40  			d.enableHeight = -1
    41  		}
    42  		// 对配置的插件,默认设置开启高度为0
    43  		for _, name := range cfg.EnableTypes {
    44  			if d, ok := drivers[name]; ok {
    45  				d.enableHeight = 0
    46  			}
    47  		}
    48  	}
    49  
    50  	// 配置中指定了启用高度,覆盖设置
    51  	for name, enableHeight := range cfg.EnableHeight {
    52  		// enableHeight如果为-1,表示插件本身在配置中未开启,则enableHeight的配置无效
    53  		if d, ok := drivers[name]; ok && d.enableHeight >= 0 {
    54  			d.enableHeight = enableHeight
    55  		}
    56  	}
    57  
    58  	//插件初始化
    59  	for name, driver := range drivers {
    60  		if driver.initFunc != nil {
    61  			driver.initFunc(subCfg[name])
    62  		}
    63  	}
    64  }
    65  
    66  //Register 注册加密算法,支持选项,设置typeID相关参数
    67  func Register(name string, crypto Crypto, options ...Option) {
    68  	driverMutex.Lock()
    69  	defer driverMutex.Unlock()
    70  
    71  	driver := &Driver{crypto: crypto}
    72  
    73  	for _, option := range options {
    74  		if err := option(driver); err != nil {
    75  			panic(err)
    76  		}
    77  	}
    78  
    79  	// 未指定情况,系统生成对应的typeID
    80  	if driver.typeID <= 0 {
    81  		driver.typeID = GenDriverTypeID(name)
    82  	}
    83  
    84  	d, ok := drivers[name]
    85  	// cgo和go版本同时存在时,允许重复注册
    86  	if ok {
    87  		if driver.isCGO == d.isCGO {
    88  			panic("crypto: Register duplicate driver name, name=" + name)
    89  		}
    90  		// 检测cgo和go版本typeID值是否一致
    91  		if driver.typeID != d.typeID {
    92  			panic(fmt.Sprintf("crypto: Register differt type id in cgo version,"+
    93  				" typeID=[%d, %d]", d.typeID, driver.typeID))
    94  		}
    95  		// 替换go版本,使用性能更好的cgo版本
    96  		if driver.isCGO {
    97  			drivers[name] = driver
    98  		}
    99  		return
   100  	}
   101  
   102  	if _, ok := driversType[driver.typeID]; ok {
   103  		// 有重复直接显示报错, 这里有可能是系统自动生成重复TypeID导致的,不能隐式解决重复
   104  		// 因为不同插件组合方式,冲突的情况也不同,需要及时报错并采用手动指定方式解决
   105  		panic(fmt.Sprintf("crypto: Register duplicate driver typeID=%d, "+
   106  			"use WithOptionTypeID for manual setting", driver.typeID))
   107  	}
   108  	drivers[name] = driver
   109  	driversType[driver.typeID] = name
   110  }
   111  
   112  //GetName 获取name
   113  func GetName(ty int) string {
   114  
   115  	name, ok := driversType[int32(ty)]
   116  	if ok {
   117  		return name
   118  	}
   119  	return "unknown"
   120  }
   121  
   122  //GetType 获取type
   123  func GetType(name string) int {
   124  	if driver, ok := drivers[name]; ok {
   125  		return int(driver.typeID)
   126  	}
   127  	return 0
   128  }
   129  
   130  //New new
   131  func New(name string) (Crypto, error) {
   132  
   133  	c, ok := drivers[name]
   134  	if !ok {
   135  		return nil, fmt.Errorf("unknown driver %q", name)
   136  	}
   137  	return c.crypto, nil
   138  }
   139  
   140  // IsEnable 根据高度判定是否开启
   141  func IsEnable(name string, height int64) bool {
   142  
   143  	d, ok := drivers[name]
   144  	return ok && d.enableHeight >= 0 && d.enableHeight <= height
   145  }