framework

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: May 11, 2022 License: Apache-2.0, MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ADDRESSVERSION_V1 = byte(0x91)
)

Variables

View Source
var (
	// 随机数算法标识
	RANDOM_ALGORITHM = 0x1000

	// 哈希数算法标识
	HASH_ALGORITHM = 0x2000

	// 签名算法标识
	SIGNATURE_ALGORITHM = 0x4000

	// 加密算法标识
	ENCRYPTION_ALGORITHM = 0x8000

	/**
	 * 扩展密码算法标识;
	 * 表示除了
	 * {@link #RANDOM_ALGORITHM}、{@link #HASH_ALGORITHM}、{@link #SIGNATURE_ALGORITHM}、{@link #ENCRYPTION_ALGORITHM}
	 * 之外的其它非标准分类的密码算法,诸如加法同态算法、多方求和算法等;
	 */
	EXT_ALGORITHM = 0x0000

	// 非对称密钥标识
	ASYMMETRIC_KEY = 0x0100

	// 对称密钥标识
	SYMMETRIC_KEY = 0x0200

	// 算法编码的字节长度;等同于 {@link #getCodeBytes(CryptoAlgorithm)} 返回的字节数组的长度
	CODE_SIZE = 2
)
View Source
var (
	// 非对称密钥的公钥
	PUBLIC = CryptoKeyType{0x01}
	// 非对称密钥的私钥
	PRIVATE = CryptoKeyType{0x02}
	// 对称密钥
	SYMMETRIC = CryptoKeyType{0x03}
)
View Source
var ALGORYTHM_CODE_SIZE = CODE_SIZE

算法标识符的长度

View Source
var (
	KEY_TYPE_BYTES = 1
)

Functions

func DecodeAlgorithm

func DecodeAlgorithm(cryptoBytes []byte) int16

func EncodeBytes

func EncodeBytes(algorithm int16, rawCryptoBytes []byte) []byte

func EncodeKeyBytes

func EncodeKeyBytes(rawKeyBytes []byte, keyType CryptoKeyType) []byte

func GenerateAddress

func GenerateAddress(pubKey *PubKey) []byte

从公钥生成地址

Types

type AsymmetricCiphertext

type AsymmetricCiphertext struct {
	BaseCryptoBytes
}

func NewAsymmetricCiphertext

func NewAsymmetricCiphertext(algorithm CryptoAlgorithm, rawCryptoBytes []byte) *AsymmetricCiphertext

func ParseAsymmetricCiphertext

func ParseAsymmetricCiphertext(cryptoBytes []byte) (*AsymmetricCiphertext, error)

func (AsymmetricCiphertext) GetRawCiphertext

func (s AsymmetricCiphertext) GetRawCiphertext() []byte

type AsymmetricEncryptionFunction

type AsymmetricEncryptionFunction interface {
	AsymmetricKeypairGenerator
	CryptoFunction

	/**
	 * 加密;
	 *
	 * @param data
	 * @return
	 */
	Encrypt(pubKey *PubKey, data []byte) (*AsymmetricCiphertext, error)

	/**
	 * 解密;
	 *
	 * @param privKey
	 * @param ciphertext
	 * @return
	 */
	Decrypt(privKey *PrivKey, ciphertext *AsymmetricCiphertext) ([]byte, error)

	/**
	 * 使用私钥恢复公钥;
	 *
	 * @param privKey PrivKey形式的私钥信息
	 * @return PubKey形式的公钥信息
	 */
	RetrievePubKey(privKey *PrivKey) (*PubKey, error)

	/**
	 * 校验私钥格式是否满足要求;
	 *
	 * @param privKeyBytes 包含算法标识、密钥掩码和私钥的字节数组
	 * @return 是否满足指定算法的私钥格式
	 */
	SupportPrivKey(privKeyBytes []byte) bool

	/**
	 * 将字节数组形式的私钥转换成PrivKey格式;
	 *
	 * @param privKeyBytes 包含算法标识和私钥的字节数组
	 * @return PrivKey形式的私钥
	 */
	ParsePrivKey(privKeyBytes []byte) (*PrivKey, error)

	/**
	 * 校验公钥格式是否满足要求;
	 *
	 * @param pubKeyBytes 包含算法标识、密钥掩码和公钥的字节数组
	 * @return 是否满足指定算法的公钥格式
	 */
	SupportPubKey(pubKeyBytes []byte) bool

	/**
	 * 将字节数组形式的密钥转换成PubKey格式;
	 *
	 * @param pubKeyBytes 包含算法标识和公钥的字节数组
	 * @return PubKey形式的公钥
	 */
	ParsePubKey(pubKeyBytes []byte) (*PubKey, error)

	/**
	 * 校验密文格式是否满足要求;
	 *
	 * @param ciphertextBytes 包含算法标识和密文的字节数组
	 * @return 是否满足指定算法的密文格式
	 */
	SupportCiphertext(ciphertextBytes []byte) bool

	/**
	 * 将字节数组形式的密文转换成AsymmetricCiphertext格式;
	 *
	 * @param ciphertextBytes 包含算法标识和密文的字节数组
	 * @return AsymmetricCiphertext形式的签名摘要
	 */
	ParseCiphertext(ciphertextBytes []byte) (*AsymmetricCiphertext, error)
}

type AsymmetricKeypair

type AsymmetricKeypair struct {
	PubKey  *PubKey
	PrivKey *PrivKey
}

非对称秘钥

func NewAsymmetricKeypair

func NewAsymmetricKeypair(pubKey *PubKey, privKey *PrivKey) (*AsymmetricKeypair, error)

type AsymmetricKeypairGenerator

type AsymmetricKeypairGenerator interface {
	GenerateKeypair() (*AsymmetricKeypair, error)
	GenerateKeypairWithSeed(seed []byte) (*AsymmetricKeypair, error)
}

*

  • @Author: imuge
  • @Date: 2020/4/28 2:16 下午

type BaseCryptoBytes

type BaseCryptoBytes struct {
	*bytes.Bytes
	// contains filtered or unexported fields
}

func NewBaseCryptoBytes

func NewBaseCryptoBytes(algorithm CryptoAlgorithm, rawCryptoBytes []byte) BaseCryptoBytes

func ParseBaseCryptoBytes

func ParseBaseCryptoBytes(cryptoBytes []byte, support func(CryptoAlgorithm) bool) (*BaseCryptoBytes, error)

func (BaseCryptoBytes) GetAlgorithm

func (b BaseCryptoBytes) GetAlgorithm() int16

func (BaseCryptoBytes) GetRawCryptoBytes

func (b BaseCryptoBytes) GetRawCryptoBytes() (*bytes.Slice, error)

type BaseCryptoKey

type BaseCryptoKey struct {
	BaseCryptoBytes
	// contains filtered or unexported fields
}

func NewBaseCryptoKey

func NewBaseCryptoKey(algorithm CryptoAlgorithm, rawKeyBytes []byte, keyType CryptoKeyType) BaseCryptoKey

func ParseBaseCryptoKey

func ParseBaseCryptoKey(cryptoBytes []byte) (*BaseCryptoKey, error)

func (BaseCryptoKey) GetKeyType

func (b BaseCryptoKey) GetKeyType() CryptoKeyType

func (BaseCryptoKey) GetRawKeyBytes

func (b BaseCryptoKey) GetRawKeyBytes() ([]byte, error)

type Ciphertext

type Ciphertext interface {
	CryptoBytes

	// 原始的密文数据
	GetRawCiphertext() []byte
}

密文

type CryptoAlgorithm

type CryptoAlgorithm struct {
	Code int16  `primitiveType:"INT16"`
	Name string `primitiveType:"TEXT"`
}

func DefinExt

func DefinExt(name string, uid byte) CryptoAlgorithm

*

  • 声明一项扩展的密码算法; *
  • @param name 算法名称;
  • @param uid 算法ID;需要在同类算法中保持唯一性;
  • @return

func DefineAsymmetricEncryption

func DefineAsymmetricEncryption(name string, uid byte) CryptoAlgorithm

*

  • 声明一项非对称加密算法; *
  • @param name 算法名称;
  • @param uid 算法ID;需要在同类算法中保持唯一性;
  • @return

func DefineHash

func DefineHash(name string, uid byte) CryptoAlgorithm

*

  • 声明一项哈希算法; *
  • @param name 算法名称;
  • @param uid 算法ID;需要在同类算法中保持唯一性;
  • @return

func DefineRandom

func DefineRandom(name string, uid byte) CryptoAlgorithm

*

  • 声明一项随机数算法; *
  • @param name 算法名称;
  • @param uid 算法ID;需要在同类算法中保持唯一性;
  • @return

func DefineSignature

func DefineSignature(name string, encryptable bool, uid byte) CryptoAlgorithm

*

  • 声明一项非对称密码算法; *
  • @param name 算法名称;
  • @param uid 算法ID;需要在同类算法中保持唯一性;
  • @return

func DefineSymmetricEncryption

func DefineSymmetricEncryption(name string, uid byte) CryptoAlgorithm

*

  • 声明一项对称密码算法; *
  • @param name 算法名称;
  • @param uid 算法ID;需要在同类算法中保持唯一性;
  • @return

func (CryptoAlgorithm) ContractCode

func (ca CryptoAlgorithm) ContractCode() int32

func (CryptoAlgorithm) ContractName

func (ca CryptoAlgorithm) ContractName() string

func (CryptoAlgorithm) Description

func (ca CryptoAlgorithm) Description() string

func (CryptoAlgorithm) Equals

func (ca CryptoAlgorithm) Equals(algorithm CryptoAlgorithm) bool

func (CryptoAlgorithm) HasAsymmetricKey

func (ca CryptoAlgorithm) HasAsymmetricKey() bool

func (CryptoAlgorithm) HasSymmetricKey

func (ca CryptoAlgorithm) HasSymmetricKey() bool

func (CryptoAlgorithm) IsAsymmetricEncryptionAlgorithm

func (ca CryptoAlgorithm) IsAsymmetricEncryptionAlgorithm() bool

func (CryptoAlgorithm) IsEncryptionAlgorithm

func (ca CryptoAlgorithm) IsEncryptionAlgorithm() bool

func (CryptoAlgorithm) IsExtAlgorithm

func (ca CryptoAlgorithm) IsExtAlgorithm() bool

func (CryptoAlgorithm) IsHashAlgorithm

func (ca CryptoAlgorithm) IsHashAlgorithm() bool

func (CryptoAlgorithm) IsRandomAlgorithm

func (ca CryptoAlgorithm) IsRandomAlgorithm() bool

func (CryptoAlgorithm) IsSignatureAlgorithm

func (ca CryptoAlgorithm) IsSignatureAlgorithm() bool

func (CryptoAlgorithm) IsSymmetricEncryptionAlgorithm

func (ca CryptoAlgorithm) IsSymmetricEncryptionAlgorithm() bool

func (CryptoAlgorithm) Match

func (ca CryptoAlgorithm) Match(algorithmBytes []byte, offset int) bool

func (CryptoAlgorithm) ToString

func (ca CryptoAlgorithm) ToString() string

type CryptoBytes

type CryptoBytes interface {
	bytes.BytesSerializable

	/**
	 * 算法;
	 *
	 * @return
	 */
	GetAlgorithm() int16
}

type CryptoDigest

type CryptoDigest interface {
	CryptoBytes

	// 原始的摘要数据
	GetRawDigest() []byte
}

摘要

type CryptoFunction

type CryptoFunction interface {
	GetAlgorithm() CryptoAlgorithm
}

*

  • @Author: imuge
  • @Date: 2020/4/28 2:13 下午

type CryptoKey

type CryptoKey interface {
	CryptoBytes

	// 密钥的类型
	GetKeyType() CryptoKeyType

	// 原始的密钥数据
	GetRawKeyBytes() ([]byte, error)
}

秘钥

type CryptoKeyType

type CryptoKeyType struct {
	Code byte
}

func DecodeKeyType

func DecodeKeyType(cryptoBytes bytes.Slice) (CryptoKeyType, error)

func GetCryptoKeyType

func GetCryptoKeyType(code byte) (CryptoKeyType, error)

type CryptoService

type CryptoService interface {
	GetFunctions() []CryptoFunction
}

type HashDigest

type HashDigest struct {
	BaseCryptoBytes
}

func NewHashDigest

func NewHashDigest(algorithm CryptoAlgorithm, rawCryptoBytes []byte) *HashDigest

func ParseHashDigest

func ParseHashDigest(cryptoBytes []byte) (*HashDigest, error)

func (HashDigest) GetRawDigest

func (s HashDigest) GetRawDigest() []byte

type HashFunction

type HashFunction interface {
	CryptoFunction

	/**
	 * 计算指定数据的 hash;
	 *
	 * @param data
	 * @return
	 */
	Hash(data []byte) *HashDigest

	/**
	 * 校验 hash 摘要与指定的数据是否匹配;
	 *
	 * @param digest
	 * @param data
	 * @return
	 */
	Verify(digest *HashDigest, data []byte) bool

	/**
	 * 校验字节数组形式的hash摘要的格式是否满足要求;
	 *
	 * @param digestBytes 包含算法标识和hash摘要的字节数组
	 * @return 是否满足指定算法的hash摘要格式
	 */
	SupportHashDigest(digestBytes []byte) bool

	/**
	 * 将字节数组形式的hash摘要转换成HashDigest格式;
	 *
	 * @param digestBytes 包含算法标识和hash摘要的字节数组
	 * @return HashDigest形式的hash摘要
	 */
	ParseHashDigest(digestBytes []byte) (*HashDigest, error)
}

type PrivKey

type PrivKey struct {
	BaseCryptoKey
}

私钥

func NewPrivKey

func NewPrivKey(algorithm CryptoAlgorithm, rawKeyBytes []byte) *PrivKey

func ParsePrivKey

func ParsePrivKey(keyBytes []byte) (*PrivKey, error)

type PubKey

type PubKey struct {
	BaseCryptoKey
}

公钥

func NewPubKey

func NewPubKey(algorithm CryptoAlgorithm, rawKeyBytes []byte) *PubKey

func ParsePubKey

func ParsePubKey(keyBytes []byte) (*PubKey, error)

func (PubKey) GetRawKeyBytes added in v1.3.3

func (b PubKey) GetRawKeyBytes() []byte

type RandomFunction

type RandomFunction interface {
	CryptoFunction

	Generate(seed int64) RandomGenerator
}

type RandomGenerator

type RandomGenerator interface {
	NextBytes(size int) []byte
}

type SignatureDigest

type SignatureDigest struct {
	BaseCryptoBytes
}

func NewSignatureDigest

func NewSignatureDigest(algorithm CryptoAlgorithm, rawCryptoBytes []byte) *SignatureDigest

func ParseSignatureDigest

func ParseSignatureDigest(cryptoBytes []byte) (*SignatureDigest, error)

func (SignatureDigest) GetRawDigest

func (s SignatureDigest) GetRawDigest() []byte

type SignatureFunction

type SignatureFunction interface {
	AsymmetricKeypairGenerator
	CryptoFunction

	/**
	 * 计算指定数据的 hash;
	 *
	 * @param data 被签名消息
	 * @return SignatureDigest形式的签名摘要
	 */
	Sign(privKey *PrivKey, data []byte) (*SignatureDigest, error)

	/**
	 * 校验签名摘要和数据是否一致;
	 *
	 * @param digest 待验证的签名摘要
	 * @param data 被签名信息
	 * @return 是否验证通过
	 */
	Verify(pubKey *PubKey, data []byte, digest *SignatureDigest) bool

	/**
	 * 使用私钥恢复公钥;
	 *
	 * @param privKey PrivKey形式的私钥信息
	 * @return PubKey形式的公钥信息
	 */
	RetrievePubKey(privKey *PrivKey) (*PubKey, error)

	/**
	 * 校验私钥格式是否满足要求;
	 *
	 * @param privKeyBytes 包含算法标识、密钥掩码和私钥的字节数组
	 * @return 是否满足指定算法的私钥格式
	 */
	SupportPrivKey(privKeyBytes []byte) bool

	/**
	 * 将字节数组形式的私钥转换成PrivKey格式;
	 *
	 * @param privKeyBytes 包含算法标识、密钥掩码和私钥的字节数组
	 * @return PrivKey形式的私钥
	 */
	ParsePrivKey(privKeyBytes []byte) (*PrivKey, error)

	/**
	 * 校验公钥格式是否满足要求;
	 *
	 * @param pubKeyBytes 包含算法标识、密钥掩码和公钥的字节数组
	 * @return 是否满足指定算法的公钥格式
	 */
	SupportPubKey(pubKeyBytes []byte) bool

	/**
	 * 将字节数组形式的密钥转换成PubKey格式;
	 *
	 * @param pubKeyBytes 包含算法标识、密钥掩码和公钥的字节数组
	 * @return PubKey形式的公钥
	 */
	ParsePubKey(pubKeyBytes []byte) (*PubKey, error)

	SupportDigest(digestBytes []byte) bool

	/**
	 * 将字节数组形式的签名摘要转换成SignatureDigest格式;
	 *
	 * @param digestBytes 包含算法标识和签名摘要的字节数组
	 * @return SignatureDigest形式的签名摘要
	 */
	ParseDigest(digestBytes []byte) (*SignatureDigest, error)

	// 从证书解析公钥
	RetrievePubKeyFromCA(cert *ca.Certificate) (*PubKey, error)

	// 从私钥文件解析私钥
	RetrievePrivKey(privateKey string) (*PrivKey, error)

	// 从私钥文件解析加密私钥
	RetrieveEncrypedPrivKey(privateKey string, pwd []byte) (*PrivKey, error)
}

type SymmetricCiphertext

type SymmetricCiphertext struct {
	BaseCryptoBytes
}

func NewSymmetricCiphertext

func NewSymmetricCiphertext(algorithm CryptoAlgorithm, rawCryptoBytes []byte) *SymmetricCiphertext

func ParseSymmetricCiphertext

func ParseSymmetricCiphertext(cryptoBytes []byte) (*SymmetricCiphertext, error)

func (SymmetricCiphertext) GetRawCiphertext

func (s SymmetricCiphertext) GetRawCiphertext() []byte

type SymmetricEncryptionFunction

type SymmetricEncryptionFunction interface {
	SymmetricKeyGenerator
	CryptoFunction

	/**
	 * 加密;
	 *
	 * @param key 密钥;
	 * @param data 明文;
	 * @return
	 */
	Encrypt(key *SymmetricKey, data []byte) (*SymmetricCiphertext, error)

	/**
	 * 解密;
	 *
	 * @param key 密钥;
	 * @param ciphertext 密文;
	 * @return
	 */
	Decrypt(key *SymmetricKey, ciphertext *SymmetricCiphertext) ([]byte, error)

	/**
	 * 校验对称密钥格式是否满足要求;
	 *
	 * @param symmetricKeyBytes 包含算法标识、密钥掩码和对称密钥的字节数组
	 * @return 是否满足指定算法的对称密钥格式
	 */
	SupportSymmetricKey(symmetricKeyBytes []byte) bool

	/**
	 * 将字节数组形式的密钥转换成SymmetricKey格式;
	 *
	 * @param symmetricKeyBytes 包含算法标识、密钥掩码和对称密钥的字节数组
	 * @return SymmetricKey形式的对称密钥
	 */
	ParseSymmetricKey(symmetricKeyBytes []byte) (*SymmetricKey, error)

	/**
	 * 校验密文格式是否满足要求;
	 *
	 * @param ciphertextBytes 包含算法标识和密文的字节数组
	 * @return 是否满足指定算法的密文格式
	 */
	SupportCiphertext(ciphertextBytes []byte) bool

	ParseCiphertext(ciphertextBytes []byte) (*SymmetricCiphertext, error)
}

type SymmetricKey

type SymmetricKey struct {
	BaseCryptoKey
}

对称秘钥

func NewSymmetricKey

func NewSymmetricKey(algorithm CryptoAlgorithm, rawKeyBytes []byte) *SymmetricKey

func ParseSymmetricKey

func ParseSymmetricKey(keyBytes []byte) (*SymmetricKey, error)

func (SymmetricKey) GetRawKeyBytes added in v1.3.3

func (b SymmetricKey) GetRawKeyBytes() []byte

type SymmetricKeyGenerator

type SymmetricKeyGenerator interface {
	GenerateSymmetricKey() *SymmetricKey
}

*

  • @Author: imuge
  • @Date: 2020/4/28 2:14 下午

Jump to

Keyboard shortcuts

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