/
util.go
129 lines (112 loc) · 2.9 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package crypto
import (
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"github.com/tjfoc/gmsm/sm3"
"golang.org/x/crypto/ripemd160"
)
//Sha256 加密算法
func Sha256(bytes []byte) []byte {
hasher := sha256.New()
hasher.Write(bytes)
return hasher.Sum(nil)
}
//Ripemd160 加密算法
func Ripemd160(bytes []byte) []byte {
hasher := ripemd160.New()
hasher.Write(bytes)
return hasher.Sum(nil)
}
//Sm3Hash 加密算法
func Sm3Hash(msg []byte) []byte {
c := sm3.New()
c.Write(msg)
return c.Sum(nil)
}
// BasicValidation 公私钥数据签名验证基础实现
func BasicValidation(c Crypto, msg, pub, sig []byte) error {
pubKey, err := c.PubKeyFromBytes(pub)
if err != nil {
return err
}
s, err := c.SignatureFromBytes(sig)
if err != nil {
return err
}
if !pubKey.VerifyBytes(msg, s) {
return ErrSign
}
return nil
}
//ToAggregate 判断签名是否可以支持聚合签名,并且返回聚合签名的接口
func ToAggregate(c Crypto) (AggregateCrypto, error) {
if aggr, ok := c.(AggregateCrypto); ok {
return aggr, nil
}
return nil, ErrNotSupportAggr
}
// WithRegOptionCGO 设置为CGO版本
func WithRegOptionCGO() RegOption {
return func(d *Driver) error {
d.isCGO = true
return nil
}
}
// WithRegOptionDefaultDisable 设置默认不启用
func WithRegOptionDefaultDisable() RegOption {
return func(d *Driver) error {
d.enable = false
return nil
}
}
const (
// MaxManualTypeID 手动指定ID最大值 4095
MaxManualTypeID = 1<<12 - 1
)
// WithRegOptionTypeID 手动指定typeID, 不指定情况,系统将根据name自动生成typeID
func WithRegOptionTypeID(id int32) RegOption {
return func(d *Driver) error {
if id <= 0 {
return errors.New("TypeIDMustPositive")
}
if id > MaxManualTypeID {
return fmt.Errorf("TypeIDMustLessThan %d", MaxManualTypeID+1)
}
d.typeID = id
return nil
}
}
// WithRegOptionInitFunc 设置插件初始化接口
func WithRegOptionInitFunc(fn DriverInitFunc) RegOption {
return func(d *Driver) error {
if fn == nil {
return errors.New("NilInitFunc")
}
d.initFunc = fn
return nil
}
}
// GenDriverTypeID 根据名称生成driver type id
func GenDriverTypeID(name string) int32 {
//分别生成高16位和低16位, 错开地址位
highVal := int32(binary.BigEndian.Uint32(Sha256([]byte(name)))%MaxManualTypeID+1) << 16
lowVal := int32(binary.BigEndian.Uint32(Ripemd160([]byte(name))) % MaxManualTypeID)
return highVal | lowVal
}
// WithLoadOptionEnableCheck 在New阶段根据当前区块高度进行插件使能检测
func WithLoadOptionEnableCheck(blockHeight int64) LoadOption {
return func(d *Driver) error {
if blockHeight < 0 {
return nil
}
if d.enable && d.enableHeight >= 0 && blockHeight >= d.enableHeight {
return nil
}
return ErrDriverNotEnable
}
}