crypto

package
v1.65.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2021 License: BSD-3-Clause Imports: 13 Imported by: 217

README

crypto

功能

  • 支持 ed25519, secp256k1, sm2
  • 统一的 PrivKey,Pubkey, Signature 接口, 详见 crypto.go

依赖

  • sm2 编译依赖 gmssl 2.0 版本
  • 安装:bash ./deps/install_gmssl.sh

Documentation

Overview

Package crypto 加解密、签名接口定义

Index

Constants

View Source
const MaxManualTypeID = math.MaxUint16 - 1

MaxManualTypeID 手动指定ID最大值 65534

Variables

View Source
var (
	//ErrNotSupportAggr 不支持聚合签名
	ErrNotSupportAggr = errors.New("AggregateCrypto not support")
	//ErrSign 签名错误
	ErrSign = errors.New("error signature")
)

Functions

func BasicValidation added in v1.65.1

func BasicValidation(c Crypto, msg, pub, sig []byte) error

BasicValidation 公私钥数据签名验证基础实现

func CRandBytes

func CRandBytes(numBytes int) []byte

CRandBytes This uses the OS and the Seed(s).

func CRandHex

func CRandHex(numDigits int) string

CRandHex RandHex(24) gives 96 bits of randomness, strong enough for most purposes.

func CReader

func CReader() io.Reader

CReader Returns a crand.Reader mixed with user-supplied entropy

func GenDriverTypeID added in v1.65.1

func GenDriverTypeID(name string) int32

GenDriverTypeID 根据名称生成driver type id

func GetName

func GetName(ty int) string

GetName 获取name

func GetType

func GetType(name string) int

GetType 获取type

func Init added in v1.65.1

func Init(cfg *Config, subCfg map[string][]byte)

Init init crypto

func IsEnable added in v1.65.1

func IsEnable(name string, height int64) bool

IsEnable 根据高度判定是否开启

func MixEntropy

func MixEntropy(seedBytes []byte)

MixEntropy Mix additional bytes of randomness, e.g. from hardware, user-input, etc. It is OK to call it multiple times. It does not diminish security.

func Register

func Register(name string, crypto Crypto, options ...Option)

Register 注册加密算法,支持选项,设置typeID相关参数

func Ripemd160

func Ripemd160(bytes []byte) []byte

Ripemd160 加密算法

func Sha256

func Sha256(bytes []byte) []byte

Sha256 加密算法

func Sm3Hash

func Sm3Hash(msg []byte) []byte

Sm3Hash 加密算法

Types

type AggregateCrypto added in v1.65.0

type AggregateCrypto interface {
	Aggregate(sigs []Signature) (Signature, error)
	AggregatePublic(pubs []PubKey) (PubKey, error)
	VerifyAggregatedOne(pubs []PubKey, m []byte, sig Signature) error
	VerifyAggregatedN(pubs []PubKey, ms [][]byte, sig Signature) error
}

AggregateCrypto 聚合签名

func ToAggregate added in v1.65.0

func ToAggregate(c Crypto) (AggregateCrypto, error)

ToAggregate 判断签名是否可以支持聚合签名,并且返回聚合签名的接口

type CertSignature

type CertSignature struct {
	Signature []byte
	Cert      []byte
}

CertSignature 签名

type Config added in v1.65.1

type Config struct {
	//支持只指定若干加密类型,不配置默认启用所有的加密插件, 如 types=["secp256k1", "sm2"]
	EnableTypes []string `json:"enableTypes,omitempty"`
	//支持对EnableTypes的每个加密插件分别设置启用高度, 不配置采用内置的启用高度
	// [crypto.enableHeight]
	// secp256k1=0
	EnableHeight map[string]int64 `json:"enableHeight,omitempty"`
}

Config crypto模块配置

type Crypto

type Crypto interface {
	GenKey() (PrivKey, error)
	SignatureFromBytes([]byte) (Signature, error)
	PrivKeyFromBytes([]byte) (PrivKey, error)
	PubKeyFromBytes([]byte) (PubKey, error)
	Validate(msg, pub, sig []byte) error
}

Crypto 加密

func New

func New(name string) (Crypto, error)

New new

type Driver added in v1.65.1

type Driver struct {
	// contains filtered or unexported fields
}

Driver 加密插件及相关信息

type DriverInitFunc added in v1.65.1

type DriverInitFunc func(jsonCfg []byte)

DriverInitFunc 插件初始化接口,参数是序列化的json数据,需要unmarshal为自定义的结构

type Option added in v1.65.1

type Option func(*Driver) error

Option 注册Driver时可选参数,设置相关参数默认值

func WithOptionCGO added in v1.65.1

func WithOptionCGO() Option

WithOptionCGO 设置为CGO版本

func WithOptionDefaultDisable added in v1.65.1

func WithOptionDefaultDisable() Option

WithOptionDefaultDisable 设置默认不启用

func WithOptionInitFunc added in v1.65.1

func WithOptionInitFunc(fn DriverInitFunc) Option

WithOptionInitFunc 设置插件初始化接口

func WithOptionTypeID added in v1.65.1

func WithOptionTypeID(id int32) Option

WithOptionTypeID 手动指定typeID, 不指定情况,系统将根据name自动生成typeID

type PrivKey

type PrivKey interface {
	Bytes() []byte
	Sign(msg []byte) Signature
	PubKey() PubKey
	Equals(PrivKey) bool
}

PrivKey 私钥

type PubKey

type PubKey interface {
	Bytes() []byte
	KeyString() string
	VerifyBytes(msg []byte, sig Signature) bool
	Equals(PubKey) bool
}

PubKey 公钥

type Signature

type Signature interface {
	Bytes() []byte
	IsZero() bool
	String() string
	Equals(Signature) bool
}

Signature 签名

Directories

Path Synopsis
Package sha3 implements the SHA-3 fixed-output-length hash functions and the SHAKE variable-output-length hash functions defined by FIPS-202.
Package sha3 implements the SHA-3 fixed-output-length hash functions and the SHAKE variable-output-length hash functions defined by FIPS-202.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL