crypto

package
v0.0.0-...-5fb9e85 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2022 License: Apache-2.0 Imports: 26 Imported by: 3

Documentation

Overview

Package crypto

@author: xwc1125

Package crypto

@author: xwc1125

Package crypto

@author: xwc1125

Index

Constants

View Source
const (
	KeyType_RSA     int32 = iota
	KeyType_Ed25519 int32 = 1
	KeyType_P256    int32 = 3
	KeyType_S256    int32 = 5
	KeyType_SM2     int32 = 6
	KeyType_P384    int32 = 7
	KeyType_P521    int32 = 8
)

Variables

View Source
var (
	UNKNOWN = CryptoType{"UNKNOWN", -1}
	RSA     = CryptoType{"RSA", KeyType_RSA}
	Ed25519 = CryptoType{"Ed25519", KeyType_Ed25519}
	P256    = CryptoType{"P-256", KeyType_P256}
	P384    = CryptoType{"P-384", KeyType_P384}
	P521    = CryptoType{"P-521", KeyType_P521}
	S256    = CryptoType{"S-256", KeyType_S256}
	SM2P256 = CryptoType{"SM2-P-256", KeyType_SM2}
)
View Source
var (
	ErrRsaKeyTooSmall = errors.New("rsa key too small")

	FormatKeyType = "unsupported the key type:%d"
	FormatKeyName = "unsupported the key name:%s"
)
View Source
var (
	FormatUnsupported = "unsupported cure crypto. type:%d, name:%s"

	ErrPrivate = errors.New("unsupported private key type")
	ErrPublic  = errors.New("unsupported public key type")
)
View Source
var (
	MinRsaKeyBits = 1024
)

Functions

func BasicEquals

func BasicEquals(k1, k2 protocol.Key) bool

BasicEquals 比较两个key是否一样。实际是比较其bytes

func CreateAddress

func CreateAddress(b types.Address, nonce uint64) types.Address

CreateAddress 根据给定的地址及nonce生成新地址

func CreateAddress2

func CreateAddress2(b types.Address, salt [32]byte, code []byte) types.Address

CreateAddress2 根据给定地址,salt和code生成新的地址

func CryptoLabel

func CryptoLabel(cryptoType CryptoType) string

CryptoLabel 获取

func IDFromPrivateKey

func IDFromPrivateKey(sk protocol.PrivKey) (models.NodeID, error)

IDFromPrivateKey 通过私钥获取PeerID

func IDFromPublicKey

func IDFromPublicKey(pk protocol.PubKey) (models.NodeID, error)

IDFromPublicKey 根据公钥生成PeerID

func PubkeyToAddress

func PubkeyToAddress(pk crypto.PublicKey) (types.Address, error)

PubkeyToAddress 根据公钥生成地址

func RecoverPubKey

func RecoverPubKey(data []byte, signResult *signature.SignResult) (crypto.PublicKey, error)

RecoverPubKey 从签名中恢复公钥

func Sign

func Sign(data []byte, prv crypto.PrivateKey) (sig *signature.SignResult, err error)

Sign 签名数据[节点之间的签名]

func Verify

func Verify(data []byte, signResult *signature.SignResult) (bool, error)

Verify 验证签名内容 sigBytes 是signResult的bytes值

Types

type CryptoType

type CryptoType struct {
	KeyName string
	KeyType int32
}

CryptoType crypto type

func ParseKeyName

func ParseKeyName(keyName string) (CryptoType, error)

ParseKeyName 将keyName转换为CryptoType

func ParseKeyType

func ParseKeyType(keyType int32) (CryptoType, error)

ParseKeyType 将int32转换为对应的CryptoType

type JsonKey

type JsonKey struct {
	Type int32  `json:"type" mapstructure:"type"`
	Data []byte `json:"data" mapstructure:"data"`
}

JsonKey key对象

func MarshalPrivateKey

func MarshalPrivateKey(prvKey crypto.PrivateKey) (*JsonKey, error)

MarshalPrivateKey 私钥转换为JsonKey,data尽可能x509格式

func MarshalPrivateKeyX509

func MarshalPrivateKeyX509(prvKey crypto.PrivateKey) (*JsonKey, error)

func MarshalPublicKey

func MarshalPublicKey(ePub crypto.PublicKey) (*JsonKey, error)

MarshalPublicKey 将公钥转换为jsonKey格式,jsonKey.data=x509格式

func MarshalPublicKeyX509

func MarshalPublicKeyX509(ePub crypto.PublicKey) (*JsonKey, error)

func (*JsonKey) Deserialize

func (j *JsonKey) Deserialize(data []byte) error

func (*JsonKey) Serialize

func (j *JsonKey) Serialize() ([]byte, error)

func (*JsonKey) SerializeUnsafe

func (j *JsonKey) SerializeUnsafe() []byte

type KeyPair

type KeyPair struct {
	CryptoType CryptoType
	Prv        crypto.PrivateKey
	Pub        crypto.PublicKey
}

KeyPair 密钥对

func GenerateKeyPair

func GenerateKeyPair(cryptoType CryptoType) (*KeyPair, error)

GenerateKeyPair 生成p2p对应的公私钥

func ParsePrivateKeyJsonKey

func ParsePrivateKeyJsonKey(jsonPrv JsonKey) (*KeyPair, error)

ParsePrivateKeyJsonKey 解析jsonKey为对象

func ParsePrivateKeyPem

func ParsePrivateKeyPem(keyPemBytes, certPemBytes []byte, pwd []byte) (privateKey *KeyPair, err error)

ParsePrivateKeyPem parse key pem to privateKey

func ParsePublicKeyJsonKey

func ParsePublicKeyJsonKey(jsonKey JsonKey) (*KeyPair, error)

ParsePublicKeyJsonKey 解析jsonKey为对象

func ParsePublicKeyPem

func ParsePublicKeyPem(keyPemBytes []byte) (*KeyPair, error)

ParsePublicKeyPem parse key pem to publicKey

func PrivKeyWithDer

func PrivKeyWithDer(der []byte) (*KeyPair, error)

PrivKeyWithDer 将der格式转换为crypto.PrivateKey

func PubKeyWithDer

func PubKeyWithDer(der []byte) (*KeyPair, error)

PubKeyWithDer 将der格式转换为crypto.PublicKey

func ToPrivateKey

func ToPrivateKey(prvKey crypto.PrivateKey) (*KeyPair, error)

ToPrivateKey 将原生的PrivateKey转换为protocol.PrivateKey

func ToPublicKey

func ToPublicKey(pubKey crypto.PublicKey) (*KeyPair, error)

ToPublicKey 将原生的PublicKey转换为protocol.PublicKey

func UnmarshalPrivateKey

func UnmarshalPrivateKey(jsonPrvData []byte) (*KeyPair, error)

UnmarshalPrivateKey 解析JsonKeyBytes成对象,将x509格式转换为p2p私钥

func UnmarshalPublicKey

func UnmarshalPublicKey(jsonKeyBytes []byte) (*KeyPair, error)

UnmarshalPublicKey 解析jsonKeyBytes成对象,将x509格式转换为p2p公钥

Jump to

Keyboard shortcuts

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