sign

package
v0.11.11 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

* @Author: kamalyes 501893067@qq.com * @Date: 2024-10-23 17:37:08 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-10-23 17:58:07 * @FilePath: \go-toolbox\pkg\sign\aes.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-11 00:55:15 * @FilePath: \go-toolbox\pkg\sign\base.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-11 00:55:15 * @FilePath: \go-toolbox\pkg\sign\hash.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-11 00:55:15 * @FilePath: \go-toolbox\pkg\sign\rsa.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.

Index

Constants

View Source
const (
	PrivateKeyType = "PRIVATE KEY"
	PublicKeyType  = "PUBLIC KEY"
)

PEM类型常量

Variables

View Source
var (
	ErrPrivateKey         = errors.New("私钥文件错误")
	ErrPublicKey          = errors.New("公钥解析错误")
	ErrEncryptFail        = errors.New("加密失败")
	ErrDecryptFail        = errors.New("解密失败")
	ErrPemBlockTypeFail   = errors.New("PEM Block 类型不是PUBLIC KEY")
	ErrNotRsaPrivateKey   = errors.New("不是RSA Private密钥")
	ErrNotRsaPublicKeyKey = errors.New("不是RSA Public密钥")
	ErrSaltEmpty          = errors.New("盐值不能为空") // 新增盐值为空的错误
)

错误定义

View Source
var ErrUnsupportedAlgorithm = errors.New("不支持的哈希算法")

ErrUnsupportedAlgorithm 表示不支持的算法错误。

SupportHashCryptoFunc 支持的哈希算法映射

Functions

func AesDecrypt

func AesDecrypt(cipherText string, key []byte) (string, error)

AesDecrypt 解密函数

func AesEncrypt

func AesEncrypt(plainText string, key []byte) (string, error)

AesEncrypt 加密函数

func ExportRsaPrivateKeyToPEM added in v0.11.6

func ExportRsaPrivateKeyToPEM(privateKey *rsa.PrivateKey) (string, error)

ExportRsaPrivateKeyToPEM 将 RSA 私钥导出为 PEM 格式

func ExportRsaPublicKeyToPEM added in v0.11.6

func ExportRsaPublicKeyToPEM(publicKey *rsa.PublicKey) (string, error)

ExportRsaPublicKeyToPEM 将 RSA 公钥导出为 PEM 格式

func GenerateByteKey

func GenerateByteKey(password string, length int) []byte

GenerateByteKey 生成一个指定字节的密钥

func HmacSha256Base64

func HmacSha256Base64(message string, secret string) string

HmacSha256Base64 计算hmac

func HmacSha256Hex

func HmacSha256Hex(message string, secret string) string

HmacSha256Hex 字符串计算sha256之后转hex

func ParsePrivateKey added in v0.11.6

func ParsePrivateKey(content []byte) (*rsa.PrivateKey, error)

ParsePrivateKey 解析PEM格式的私钥

func ParsePublicKey added in v0.11.6

func ParsePublicKey(pemData []byte) (*rsa.PublicKey, error)

ParsePublicKey 解析 PEM 格式的公钥

func SHA256

func SHA256(text string) string

SHA256 Sha 算签名

Types

type GenericHasher added in v0.11.6

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

GenericHasher 结构体用于通用哈希计算。

func (*GenericHasher) Hash added in v0.11.6

func (g *GenericHasher) Hash(r io.Reader) (string, error)

Hash 计算哈希值并返回十六进制字符串。

type HashCryptoFunc added in v0.11.6

type HashCryptoFunc string

定义算法名称常量

const (
	AlgorithmMD5    HashCryptoFunc = "MD5"
	AlgorithmSHA1   HashCryptoFunc = "SHA1"
	AlgorithmSHA224 HashCryptoFunc = "SHA224"
	AlgorithmSHA256 HashCryptoFunc = "SHA256"
	AlgorithmSHA384 HashCryptoFunc = "SHA384"
	AlgorithmSHA512 HashCryptoFunc = "SHA512"
)

type Hasher added in v0.11.6

type Hasher interface {
	Hash(io.Reader) (string, error)
}

Hasher 接口定义了一个计算哈希值的方法。

func NewHasher added in v0.11.6

func NewHasher(algorithm HashCryptoFunc) (Hasher, error)

NewHasher 创建一个新的 GenericHasher 实例。

type RsaCrypto added in v0.11.6

type RsaCrypto interface {
	Encrypt(input []byte, salt []byte) ([]byte, error)
	Decrypt(input []byte) ([]byte, error)
	DecryptBase64(input string) ([]byte, error)
	GetPrivateKey() *rsa.PrivateKey
	GetPublicKey() *rsa.PublicKey
}

RsaCrypto 定义了RSA加解密器的接口

func NewRsaCryptoFromKeys added in v0.11.6

func NewRsaCryptoFromKeys(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, hashFunc func() hash.Hash) RsaCrypto

NewRsaCryptoFromKeys 根据公钥和私钥创建RSA加解密器

func NewRsaCryptoFromPrivateFile added in v0.11.6

func NewRsaCryptoFromPrivateFile(filePath string, hashFunc func() hash.Hash) (RsaCrypto, error)

NewRsaCryptoFromPrivateFile 根据私钥文件创建RSA加解密器

func NewRsaCryptoFromPublicPEM added in v0.11.6

func NewRsaCryptoFromPublicPEM(pemData []byte, hashFunc func() hash.Hash) (RsaCrypto, error)

NewRsaCryptoFromPublicPEM 从 PEM 格式的公钥创建 RSA 加解密器

type RsaKeyPair added in v0.11.6

type RsaKeyPair struct {
	PrivateKey *rsa.PrivateKey
	PublicKey  *rsa.PublicKey
}

RsaKeyPair 包含RSA密钥对

func GenerateRsaKeyPair added in v0.11.6

func GenerateRsaKeyPair(keySize RsaKeySize) (*RsaKeyPair, error)

GenerateRsaKeyPair 生成指定大小的 RSA 密钥对

type RsaKeySize added in v0.11.6

type RsaKeySize int

RsaKeySize 定义了支持的RSA密钥大小

const (
	RsaKeySize512  RsaKeySize = 512
	RsaKeySize1024 RsaKeySize = 1024
	RsaKeySize2048 RsaKeySize = 2048
	RsaKeySize4096 RsaKeySize = 4096
)

Jump to

Keyboard shortcuts

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