keys

package
v1.0.43 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHash

func GetHash(algorithm crypto.Hash) (hash.Hash, error)

GetHash 获取指定名称的 hash 算法

Types

type Algorithm

type Algorithm interface {
	Name() string
	Provider() Provider
}

Algorithm 表示一种密码学算法

type BlockMode

type BlockMode string

BlockMode 表示块模式

const (
	CBC BlockMode = "CBC" // Cipher Block Chaining
	EBC BlockMode = "EBC" // Electronic Codebook Book
	CTR BlockMode = "CTR" // Counter
	OCF BlockMode = "OCF" // Output FeedBack
	CFB BlockMode = "CFB" // Cipher FeedBack
)

定义块模式

type CryptOptions

type CryptOptions struct {
	Block   BlockMode
	Padding PaddingMode
	Hash    crypto.Hash // hash 算法 id
	Label   []byte
	IV      []byte // 初始化向量
}

CryptOptions 包含加密、解密的选项

type Decrypter

type Decrypter interface {
	Decrypt(data []byte, options *CryptOptions) ([]byte, *CryptOptions, error)
}

Decrypter 提供解密计算

type Encrypter

type Encrypter interface {
	Encrypt(data []byte, options *CryptOptions) ([]byte, *CryptOptions, error)
}

Encrypter 提供加密计算

type KeyAlgorithmName

type KeyAlgorithmName string

KeyAlgorithmName 表示密钥算法的名称 (例如:'RSA' | 'AES' | 'ECDSA')

func (KeyAlgorithmName) String

func (name KeyAlgorithmName) String() string

type KeyPair

type KeyPair interface {
	PublicKey() PublicKey

	PrivateKey() PrivateKey

	Export(want *KeyPairData) (*KeyPairData, error)
}

KeyPair ...

type KeyPairAlgorithm

type KeyPairAlgorithm interface {
	Algorithm

	GetGenerator() KeyPairGenerator

	GetKeyPairLoader() KeyPairLoader

	GetPublicKeyLoader() PublicKeyLoader

	GetPrivateKeyLoader() PrivateKeyLoader
}

KeyPairAlgorithm 表示一个非对称密钥对算法

type KeyPairData

type KeyPairData struct {
	Algorithm   string
	Format      string
	ContentType string
	Content     []byte
}

KeyPairData ...

type KeyPairGenerator

type KeyPairGenerator interface {
	Generate(params *KeyPairParams) (KeyPair, error)
}

KeyPairGenerator ...

type KeyPairLoader

type KeyPairLoader interface {
	Load(o *KeyPairData) (KeyPair, error)
}

KeyPairLoader ...

type KeyPairParams

type KeyPairParams struct {
	Size int // key-size in bits
}

KeyPairParams ...

type Manager

type Manager interface {

	// 根据名称,查找算法(可能有多条结果)
	Find(algorithm string) ([]Algorithm, error)

	// 根据名称,查找算法(如果选择器函数为 nil, 则适配任何条目)
	Get(algorithm string, selector func(reg *Registration) bool) (Algorithm, error)
}

Manager 是密钥算法管理器

type Padding

type Padding interface {

	// 取模式名称
	Mode() PaddingMode

	// 填充
	Pad(src []byte, blockSize int) ([]byte, error)

	// 去除填充
	Unpad(src []byte, blockSize int) ([]byte, error)
}

Padding 是提供填充方法的接口

func GetPadding

func GetPadding(mode PaddingMode) (Padding, error)

GetPadding 获取指定的填充模式

type PaddingCache

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

PaddingCache 是一个简单的填充模式缓存

func (*PaddingCache) Get

func (inst *PaddingCache) Get(want PaddingMode) (Padding, error)

Get 获取指定的填充模式

type PaddingMode

type PaddingMode string

PaddingMode 表示填充模式

const (
	NoPadding       PaddingMode = "No"
	PKCS5Padding    PaddingMode = "PKCS5"
	PKCS7Padding    PaddingMode = "PKCS7" // the default value
	PKCS1Padding    PaddingMode = "PKCS1"
	PKCS1v15Padding PaddingMode = "PKCS1v15"
	OAEP            PaddingMode = "OAEP" // optimal asymmetric encryption padding
	PSS             PaddingMode = "PSS"  // Probabilistic Signature Scheme
)

定义填充模式

func (PaddingMode) String

func (m PaddingMode) String() string

type PrivateKey

type PrivateKey interface {
	Pair() KeyPair

	NewDecrypter(options *CryptOptions) Decrypter

	NewSigner(options *SignatureOptions) Signer

	Export(want *PrivateKeyData) (*PrivateKeyData, error)
}

PrivateKey 代表私钥

type PrivateKeyData

type PrivateKeyData struct {
	Algorithm   string
	Format      string
	ContentType string
	Content     []byte
}

PrivateKeyData 代表私钥 DTO

type PrivateKeyLoader

type PrivateKeyLoader interface {
	Load(o *PrivateKeyData) (PrivateKey, error)
}

PrivateKeyLoader 代表私钥

type Provider

type Provider interface {
	Name() string
	PackageName() string
	Description() string
}

Provider 表示一个算法提供者

type PublicKey

type PublicKey interface {
	NewEncrypter(options *CryptOptions) Encrypter

	NewVerifier(options *SignatureOptions) Verifier

	Export(want *PublicKeyData) (*PublicKeyData, error)
}

PublicKey 代表公钥

type PublicKeyData

type PublicKeyData struct {
	Algorithm   string
	Format      string
	ContentType string
	Content     []byte
}

PublicKeyData 代表公钥的 DTO

type PublicKeyLoader

type PublicKeyLoader interface {
	Load(o *PublicKeyData) (PublicKey, error)
}

PublicKeyLoader 代表公钥 loader

type Registration

type Registration struct {
	Name      string
	Enabled   bool
	Priority  int
	Algorithm Algorithm
	Provider  Provider
}

Registration 表示密钥算法的注册信息

type Registry

type Registry interface {
	ListRegistrations() []*Registration
}

Registry 代表密钥算法的注册接口

type SecretKey

type SecretKey interface {
	Export(want *SecretKeyData) (*SecretKeyData, error)

	NewEncrypter(options *CryptOptions) Encrypter

	NewDecrypter(options *CryptOptions) Decrypter

	BlockSize() int
}

SecretKey ...

type SecretKeyAlgorithm

type SecretKeyAlgorithm interface {
	Algorithm

	GetGenerator() SecretKeyGenerator

	GetLoader() SecretKeyLoader
}

SecretKeyAlgorithm 表示一个对称密钥算法

type SecretKeyData

type SecretKeyData struct {
	Algorithm   string
	Format      string
	ContentType string
	Content     []byte
}

SecretKeyData ...

type SecretKeyGenerator

type SecretKeyGenerator interface {
	Generate(params *SecretKeyParams) (SecretKey, error)
}

SecretKeyGenerator ...

type SecretKeyLoader

type SecretKeyLoader interface {
	Load(o *SecretKeyData) (SecretKey, error)
}

SecretKeyLoader ...

type SecretKeyParams

type SecretKeyParams struct {
	Size int // key-size in bits
}

SecretKeyParams ...

type Service

type Service interface {
	GetManager() Manager

	GetKeyPairAlgorithm(algorithm string, selector func(reg *Registration) bool) (KeyPairAlgorithm, error)

	GetSecretKeyAlgorithm(algorithm string, selector func(reg *Registration) bool) (SecretKeyAlgorithm, error)

	GetSignatureAlgorithm(algorithm string, selector func(reg *Registration) bool) (SignatureAlgorithm, error)
}

Service ...

type Signature

type Signature struct {
	Algorithm SignatureAlgorithmName
	Digest    []byte
	Signature []byte
}

Signature 包含签名信息

type SignatureAlgorithm

type SignatureAlgorithm interface {
	Algorithm

	Options() *SignatureOptions

	NewSigner(key PrivateKey) Signer

	NewVerifier(key PublicKey) Verifier
}

SignatureAlgorithm 表示签名算法

type SignatureAlgorithmName

type SignatureAlgorithmName string

SignatureAlgorithmName 表示签名算法的名称 (例如:'SHA384withECDSA' | 'SHA256withRSA/PSS')

func (SignatureAlgorithmName) String

func (name SignatureAlgorithmName) String() string

type SignatureOptions

type SignatureOptions struct {
	KeyAlgorithm KeyAlgorithmName // 密钥算法
	Hash         crypto.Hash      // hash 算法 id
	Padding      PaddingMode      // 填充模式
}

SignatureOptions 表示签名选项

func (*SignatureOptions) Algorithm

func (inst *SignatureOptions) Algorithm() SignatureAlgorithmName

Algorithm 把选项组合转化为 SignatureAlgorithmName

type Signer

type Signer interface {

	// 取私钥
	PrivateKey() PrivateKey

	// 生成签名
	Sign(want *Signature) (*Signature, error)
}

Signer 提供签名方法

type Verifier

type Verifier interface {

	// 取公钥
	PublicKey() PublicKey

	// 验证签名
	Verify(signature *Signature) error
}

Verifier 提供签名验证方法

Jump to

Keyboard shortcuts

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