xcrypto

package
v0.0.0-...-bc76e52 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NonceSymbols 随机字符串可用字符集
	NonceSymbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	// NonceLength 随机字符串的长度
	NonceLength = 32
)

Variables

View Source
var ErrVerification = errors.New("verification error")
View Source
var MD5 _MD5
View Source
var SHA1 _SHA1
View Source
var SHA256 _SHA256
View Source
var SHA512 _SHA512

Functions

func GenerateNonce

func GenerateNonce() (string, error)

GenerateNonce 生成一个长度为 NonceLength 的随机字符串(只包含大小写字母与数字)

func GetCertificateSerialNumber

func GetCertificateSerialNumber(certificate x509.Certificate) string

GetCertificateSerialNumber 从证书中获取证书序列号

func HMacEx

func HMacEx(h func() hash.Hash, key []byte, bs []byte) string

func HMacMD5

func HMacMD5(key []byte, bs []byte) string

func HMacSHA1

func HMacSHA1(key []byte, bs []byte) string

func HMacSHA256

func HMacSHA256(key []byte, bs []byte) string

func IsCertificateExpired

func IsCertificateExpired(certificate x509.Certificate, now time.Time) bool

IsCertificateExpired 判定证书在特定时间是否过期

func IsCertificateValid

func IsCertificateValid(certificate x509.Certificate, now time.Time) bool

IsCertificateValid 判定证书在特定时间是否有效

func LoadCertificate

func LoadCertificate(certificateStr string) (certificate *x509.Certificate, err error)

LoadCertificate 通过证书的文本内容加载证书

func LoadCertificateWithPath

func LoadCertificateWithPath(path string) (certificate *x509.Certificate, err error)

LoadCertificateWithPath 通过证书的文件路径加载证书

func LoadPrivateKey

func LoadPrivateKey(privateKeyStr string) (privateKey *rsa.PrivateKey, err error)

LoadPrivateKey 通过私钥的文本内容加载私钥 通用模式,优先按照 pkcs1 处理。

func LoadPrivateKeyWithPath

func LoadPrivateKeyWithPath(path string) (privateKey *rsa.PrivateKey, err error)

LoadPrivateKeyWithPath 通过私钥的文件路径内容加载私钥

func LoadPublicKey

func LoadPublicKey(publicKeyStr string) (publicKey *rsa.PublicKey, err error)

LoadPublicKey 通过公钥的文本内容加载公钥

func LoadPublicKeyWithPath

func LoadPublicKeyWithPath(path string) (publicKey *rsa.PublicKey, err error)

LoadPublicKeyWithPath 通过公钥的文件路径加载公钥

Types

type DefaultEncoder

type DefaultEncoder struct {
}

func (*DefaultEncoder) EncodeBytes

func (encoder *DefaultEncoder) EncodeBytes(buffer *bytes.Buffer, data []byte, opts *SignOptions) ([]byte, error)

func (*DefaultEncoder) EncodeValues

func (encoder *DefaultEncoder) EncodeValues(buffer *bytes.Buffer, values url.Values, opts *SignOptions) ([]byte, error)

EncodeValues 1、将参数名及其对应的值进行升序排序 2、将排序后的参数名及参数名使用等号进行连接,例如:a=10 3、将组合之后的参数使用&号进行连接,例如:a=10&b=20&c=30&c=31

type Encoder

type Encoder interface {
	EncodeValues(buffer *bytes.Buffer, values url.Values, opts *SignOptions) ([]byte, error)

	EncodeBytes(buffer *bytes.Buffer, data []byte, opts *SignOptions) ([]byte, error)
}

type Hash

type Hash interface {
	DoString(string) string
	DoBytes([]byte) string
}

type HashMethod

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

func NewHashMethod

func NewHashMethod(h crypto.Hash) *HashMethod

func (*HashMethod) Sign

func (hm *HashMethod) Sign(data []byte) ([]byte, error)

func (*HashMethod) Verify

func (hm *HashMethod) Verify(data []byte, signature []byte) error

type Method

type Method interface {
	Sign(data []byte) ([]byte, error)

	Verify(data []byte, signature []byte) error
}

type Option

type Option func(signer *signer)

func WithEncoder

func WithEncoder(encoder Encoder) Option

func WithMethod

func WithMethod(method Method) Option

type RSAMethod

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

func NewRSAMethod

func NewRSAMethod(h crypto.Hash, privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) *RSAMethod

func (*RSAMethod) Sign

func (method *RSAMethod) Sign(data []byte) ([]byte, error)

func (*RSAMethod) Verify

func (method *RSAMethod) Verify(data []byte, signature []byte) error

type SignOption

type SignOption func(opt *SignOptions)

func WithIgnore

func WithIgnore(keys ...string) SignOption

func WithPrefix

func WithPrefix(s string) SignOption

func WithSuffix

func WithSuffix(s string) SignOption

type SignOptions

type SignOptions struct {
	Prefix  string
	Suffix  string
	Ignores map[string]struct{}
}

type Signer

type Signer interface {
	SignValues(ctx context.Context, values url.Values, opts ...SignOption) ([]byte, error)

	SignBytes(ctx context.Context, data []byte, opts ...SignOption) ([]byte, error)

	VerifyValues(ctx context.Context, values url.Values, signature []byte, opts ...SignOption) error

	VerifyBytes(ctx context.Context, data []byte, signature []byte, opts ...SignOption) error
}

func New

func New(opts ...Option) Signer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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