Help us shape the future of Go! Take the 2026 Go Developer Survey

crypto

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 25 Imported by: 4

README

加解密算法

主要接口

本包统一定义了如下接口,便于不同算法的无缝切换和组合:

  • Cipher:对称/非对称加密接口
  • Signer:签名接口(如SM2、ECDSA等)
  • Verifier:验签接口
  • Hasher:哈希接口(如SM3、SHA256等)
  • KeyExchanger:密钥协商接口(如ECDH、SM2密钥交换)

接口定义见 interface.go,如:

// Cipher
Encrypt(plain []byte) ([]byte, error)
Decrypt(cipher []byte) ([]byte, error)
Name() string

// Signer
Sign(data []byte) (string, error)
Name() string

// Verifier
Verify(data []byte, signature string) (bool, error)
Name() string

SM2Cipher 用法示例

cipher, _ := NewSM2Cipher()
plain := []byte("hello, sm2!")

// 加解密
crypted, _ := cipher.Encrypt(plain)
decrypted, _ := cipher.Decrypt(crypted)

// 签名/验签
sig, _ := cipher.Sign(plain)
ok, _ := cipher.Verify(plain, sig)

AES 用法示例

key := []byte("1234567890abcdef") // 16字节
cipher, _ := NewAESCipher(key, nil)
plain := []byte("hello, aes!")
crypted, _ := cipher.Encrypt(plain)
decrypted, _ := cipher.Decrypt(crypted)

RSA 用法示例

cipher, _ := NewRSACipher(2048)
plain := []byte("hello, rsa!")
crypted, _ := cipher.Encrypt(plain)
decrypted, _ := cipher.Decrypt(crypted)

SM4 用法示例

key := []byte("1234567890abcdef") // 16字节
cipher, _ := NewSM4Cipher(key)
plain := []byte("hello, sm4!")
crypted, _ := cipher.Encrypt(plain)
decrypted, _ := cipher.Decrypt(crypted)

HMAC/SM3 用法示例

h := NewHMAC([]byte("key"))
mac := h.Sum([]byte("hello"))

h2 := NewSM3Hasher()
hash := h2.Sum([]byte("hello"))

全局加密器与载荷封装(encryptor.go / sign.go / payload.go)

面向"配置类敏感字段落库加密"场景的更上层封装(与上表的原语接口相互独立):

  • Encryptor:AES-256-GCM 认证加密,密文带 enc: 前缀、base64 编码, 密钥由任意长度口令经 SHA-256 派生;IsEncrypted 判定前缀;
  • 全局单例:InitGlobalEncryptor(key, enabled) / GetGlobalEncryptor / EncryptIfNeeded / DecryptIfNeeded——未配置密钥时透传明文 (DecryptIfNeeded 对无前缀串原样返回,兼容历史明文行);
  • SignData / VerifyData:绑定全局密钥的 HMAC-SHA256 签名门面 (用于签名 URL 等场景;未初始化时显式报错而非产出空签名);
  • 载荷封装:EncryptPayload(payload, reservedKeys...) 把整份配置 map 序列化加密为 {_encrypted_config, _is_encrypted} 封装, reservedKeys(如任务系统的 task_id/task_type)以明文保留供 无需解密的路由/调度层读取;DecryptPayload / HasEncryptedPayload 处理封装的解出与识别, Must* 系列为测试便捷变体。
_ = InitGlobalEncryptor(os.Getenv("APP_CRYPTO_KEY"), true)
cipherText, _ := EncryptIfNeeded("smtp-password")
plain, _ := DecryptIfNeeded(row.Config)

其它说明

  • AES/SM4:对称加密,均实现 Cipher 接口
  • RSA:非对称加密,Cipher 接口
  • HMAC/SM3/SHA256:哈希算法,实现 Hasher 接口
  • ECDSA/SM2:签名验签,Signer/Verifier 接口
  • ECDH/SM2:密钥协商,实现 KeyExchanger 接口

所有算法均可通过接口组合和替换,便于扩展和测试。

Documentation

Overview

Example (TaskConfiguration)

Example: Real-world usage with task configuration

package main

import (
	"fmt"
	"log"

	"github.com/tx7do/go-utils/crypto"
)

func main() {
	// Initialize encryption (typically done at app startup)
	crypto.InitGlobalEncryptor("production-secret-key-min-32-chars", true)

	// Simulate task configuration with sensitive data
	taskConfig := `{
		"host": "imap.gmail.com",
		"port": 993,
		"username": "user@example.com",
		"password": "super-secret-password",
		"tls": true
	}`

	// Before saving to database, encrypt the configuration
	encryptedConfig, err := crypto.EncryptIfNeeded(taskConfig)
	if err != nil {
		log.Fatal(err)
	}

	// Store in database (simulated)
	fmt.Println("Stored in DB:", crypto.IsEncrypted(encryptedConfig))

	// When reading from database, decrypt the configuration
	decryptedConfig, err := crypto.DecryptIfNeeded(encryptedConfig)
	if err != nil {
		log.Fatal(err)
	}

	// Use the decrypted configuration
	fmt.Println("Can use config:", decryptedConfig == taskConfig)

}
Output:
Stored in DB: true
Can use config: true

Index

Examples

Constants

View Source
const (
	// EncryptedConfigKey is the key used to store encrypted configuration in task payload
	EncryptedConfigKey = "_encrypted_config"
	// IsEncryptedKey indicates if the payload contains encrypted data
	IsEncryptedKey = "_is_encrypted"
)
View Source
const (
	// EncryptedPrefix is added to encrypted data to distinguish from plaintext
	EncryptedPrefix = "enc:"
)

Variables

View Source
var (
	ErrInvalidKey        = errors.New("encryption key must be at least 32 bytes")
	ErrInvalidCiphertext = errors.New("invalid ciphertext")
	ErrDecryptionFailed  = errors.New("decryption failed")
)
View Source
var DefaultAESKey = []byte("f51d66a73d8a0927")

DefaultAESKey 默认AES密钥(16字节)

Functions

func AesDecrypt

func AesDecrypt(cryptedText, key, iv []byte) ([]byte, error)

AesDecrypt AES解密

func AesEncrypt

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

AesEncrypt AES加密

func DecryptIfNeeded added in v0.0.4

func DecryptIfNeeded(ciphertext string) (string, error)

DecryptIfNeeded decrypts data if it's encrypted

Example
package main

import (
	"fmt"
	"log"

	"github.com/tx7do/go-utils/crypto"
)

func main() {
	// Initialize global encryptor
	crypto.InitGlobalEncryptor("global-secret-key", true)

	// Encrypt some data
	encrypted, _ := crypto.EncryptIfNeeded("sensitive data")

	// Decrypt using global encryptor
	decrypted, err := crypto.DecryptIfNeeded(encrypted)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Decrypted:", decrypted == "sensitive data")
}
Output:
Decrypted: true

func DecryptPayload added in v0.0.4

func DecryptPayload(payload map[string]interface{}) (map[string]interface{}, error)

DecryptPayload decrypts the payload if it contains encrypted configuration Returns the decrypted payload map

func EncryptIfNeeded added in v0.0.4

func EncryptIfNeeded(plaintext string) (string, error)

EncryptIfNeeded encrypts data if the global encryptor is initialized

Example
package main

import (
	"fmt"
	"log"

	"github.com/tx7do/go-utils/crypto"
)

func main() {
	// Initialize global encryptor
	crypto.InitGlobalEncryptor("global-secret-key", true)

	// Use global encryptor for convenience
	encrypted, err := crypto.EncryptIfNeeded("sensitive data")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Encrypted:", crypto.IsEncrypted(encrypted))
}
Output:
Encrypted: true

func EncryptPayload added in v0.0.4

func EncryptPayload(payload map[string]interface{}, reservedKeys ...string) (map[string]interface{}, error)

EncryptPayload encrypts the entire payload and returns a map with encrypted data This is used to store encrypted configuration in Redis/Asynq. reservedKeys 列出的键以明文保留在外层,供无需解密的路由/调度层读取 (如任务系统的 "task_id"/"task_type")。

安全契约:仅当真正完成加密时才置 IsEncryptedKey=true。若全局加密器未配置 (EncryptIfNeeded 返回明文原值),则不标记为已加密,避免调用方误判明文为密文 而在解密侧产生"解密失败"或静默数据错乱。

func GenerateAESKey

func GenerateAESKey(length int) ([]byte, error)

GenerateAESKey 生成AES密钥

func GenerateSM4Key

func GenerateSM4Key() ([]byte, error)

GenerateSM4Key 生成随机SM4密钥

func HasEncryptedPayload added in v0.0.4

func HasEncryptedPayload(payload map[string]interface{}) bool

HasEncryptedPayload checks if the payload contains encrypted configuration

func InitGlobalEncryptor added in v0.0.4

func InitGlobalEncryptor(key string, enabled bool) error

InitGlobalEncryptor initializes the global encryptor instance

func IsEncrypted added in v0.0.4

func IsEncrypted(data string) bool

IsEncrypted checks if the given string is encrypted (has the prefix)

Example
package main

import (
	"fmt"

	"github.com/tx7do/go-utils/crypto"
)

func main() {
	encryptedData := "enc:SGVsbG8gV29ybGQh"
	plaintextData := "Hello, World!"

	fmt.Println("Encrypted:", crypto.IsEncrypted(encryptedData))
	fmt.Println("Plaintext:", crypto.IsEncrypted(plaintextData))
}
Output:
Encrypted: true
Plaintext: false

func MustDecryptPayload added in v0.0.4

func MustDecryptPayload(payload map[string]interface{}) map[string]interface{}

MustDecryptPayload decrypts payload and panics on error (useful for testing)

func MustEncryptPayload added in v0.0.4

func MustEncryptPayload(payload map[string]interface{}, reservedKeys ...string) map[string]interface{}

MustEncryptPayload encrypts payload and panics on error (useful for testing)

func PKCS5Padding

func PKCS5Padding(plaintext []byte, blockSize int) []byte

PKCS5Padding 填充明文

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

PKCS5UnPadding 去除填充数据

func ParseECDSAPublicKey added in v0.0.3

func ParseECDSAPublicKey(data []byte) (*ecdsa.PublicKey, error)

ParseECDSAPublicKey 从 X.509 PKIX/DER 编码字节解析ECDSA公钥 是 ECDSACipher.PublicKeyBytes 的逆操作

func SHA256Sum

func SHA256Sum(data []byte) []byte

SHA256Sum 计算SHA-256哈希

func SHA512Sum

func SHA512Sum(data []byte) []byte

SHA512Sum 计算SHA-512哈希

func SignData added in v0.0.4

func SignData(data string) (string, error)

SignData 用全局加密器密钥对 data 计算 HMAC-SHA256 签名(hex 编码)。 用于签名 URL 场景(签名即凭证,需配合有效期由调用方自行拼接校验)。 全局加密器未初始化时返回错误——此时签名能力不可用,调用方应拒绝 生成签名 URL 而非降级为无签名。

func VerifyData added in v0.0.4

func VerifyData(data, signature string) bool

VerifyData 校验 SignData 生成的签名(恒定时间比较)。

Types

type AESCipher

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

func NewAESCipher

func NewAESCipher(key, iv []byte) *AESCipher

func (*AESCipher) Decrypt

func (a *AESCipher) Decrypt(cipher []byte) ([]byte, error)

func (*AESCipher) Encrypt

func (a *AESCipher) Encrypt(plain []byte) ([]byte, error)

func (*AESCipher) Name

func (a *AESCipher) Name() string

type AESGCMCipher added in v0.0.2

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

func NewAESGCMCipher added in v0.0.2

func NewAESGCMCipher(key []byte) (*AESGCMCipher, error)

NewAESGCMCipher 创建AES-GCM模式的Cipher

func (*AESGCMCipher) Decrypt added in v0.0.2

func (a *AESGCMCipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt 使用AES-GCM解密

func (*AESGCMCipher) Encrypt added in v0.0.2

func (a *AESGCMCipher) Encrypt(plain []byte) ([]byte, error)

Encrypt 使用AES-GCM加密

func (*AESGCMCipher) Name added in v0.0.2

func (a *AESGCMCipher) Name() string

type Cipher

type Cipher interface {
	// Encrypt 使用指定的密钥和初始化向量(IV)加密明文数据,返回加密后的数据
	Encrypt(plain []byte) ([]byte, error)

	// Decrypt 使用指定的密钥和初始化向量(IV)解密密文数据,返回解密后的数据
	Decrypt(cipher []byte) ([]byte, error)

	// Name 返回算法名称
	Name() string
}

Cipher 定义了加密算法的接口,支持加密和解密操作

type ECDHCipher

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

func NewECDHCipher

func NewECDHCipher() (*ECDHCipher, error)

NewECDHCipher 生成新的ECDH密钥对

func (*ECDHCipher) DeriveSharedSecret

func (e *ECDHCipher) DeriveSharedSecret(peerPubBytes []byte) ([]byte, error)

DeriveSharedSecret 计算共享密钥

func (*ECDHCipher) Name

func (e *ECDHCipher) Name() string

Name 返回算法名称

func (*ECDHCipher) PrivateKey

func (e *ECDHCipher) PrivateKey() *ecdh.PrivateKey

PrivateKey 获取私钥(仅用于安全存储,禁止对外泄露)

func (*ECDHCipher) PublicKey

func (e *ECDHCipher) PublicKey() *ecdh.PublicKey

PublicKey 获取公钥(用于导出、传输给对方)

func (*ECDHCipher) PublicKeyBytes

func (e *ECDHCipher) PublicKeyBytes() []byte

PublicKeyBytes 获取公钥字节

type ECDSACipher

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

func NewECDSACipher

func NewECDSACipher() (*ECDSACipher, error)

NewECDSACipher 生成新的ECDSA密钥对

func NewECDSACipherFromKey added in v0.0.3

func NewECDSACipherFromKey(priv *ecdsa.PrivateKey, pub *ecdsa.PublicKey) *ECDSACipher

NewECDSACipherFromKey 用已有密钥初始化

func (*ECDSACipher) Name

func (e *ECDSACipher) Name() string

Name 返回算法名称

func (*ECDSACipher) PrivateKey

func (e *ECDSACipher) PrivateKey() *ecdsa.PrivateKey

func (*ECDSACipher) PublicKeyBytes

func (e *ECDSACipher) PublicKeyBytes() []byte

PublicKeyBytes 导出公钥字节(X.509 PKIX/DER编码) 可用 ParseECDSAPublicKey 或 x509.ParsePKIXPublicKey 解析

func (*ECDSACipher) Sign

func (e *ECDSACipher) Sign(data []byte) (string, error)

Sign 签名

func (*ECDSACipher) Verify

func (e *ECDSACipher) Verify(data []byte, signature string) (bool, error)

Verify 验签

type Encryptor added in v0.0.4

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

Encryptor provides AES-256-GCM encryption/decryption functionality

func GetGlobalEncryptor added in v0.0.4

func GetGlobalEncryptor() *Encryptor

GetGlobalEncryptor returns the global encryptor instance

func NewEncryptor added in v0.0.4

func NewEncryptor(key string) (*Encryptor, error)

NewEncryptor creates a new encryptor with the given key The key will be hashed to ensure it's exactly 32 bytes for AES-256

func (*Encryptor) Decrypt added in v0.0.4

func (e *Encryptor) Decrypt(ciphertext string) (string, error)

Decrypt decrypts the ciphertext using AES-256-GCM Expects base64-encoded ciphertext with "enc:" prefix

Example
package main

import (
	"fmt"
	"log"

	"github.com/tx7do/go-utils/crypto"
)

func main() {
	// Create an encryptor with a secret key
	encryptor, err := crypto.NewEncryptor("my-secret-key")
	if err != nil {
		log.Fatal(err)
	}

	// Encrypt some data
	plaintext := `{"username":"admin","password":"secret123"}`
	encrypted, _ := encryptor.Encrypt(plaintext)

	// Decrypt it back
	decrypted, err := encryptor.Decrypt(encrypted)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Decryption successful:", decrypted == plaintext)
}
Output:
Decryption successful: true
Example (BackwardCompatibility)
package main

import (
	"fmt"
	"log"

	"github.com/tx7do/go-utils/crypto"
)

func main() {
	// Create an encryptor
	encryptor, err := crypto.NewEncryptor("my-secret-key")
	if err != nil {
		log.Fatal(err)
	}

	// Legacy unencrypted data (no "enc:" prefix)
	legacyData := `{"username":"admin","password":"old-data"}`

	// Decrypt handles unencrypted data gracefully
	decrypted, err := encryptor.Decrypt(legacyData)
	if err != nil {
		log.Fatal(err)
	}

	// Returns the data as-is for backward compatibility
	fmt.Println("Legacy data handled:", decrypted == legacyData)
}
Output:
Legacy data handled: true

func (*Encryptor) Encrypt added in v0.0.4

func (e *Encryptor) Encrypt(plaintext string) (string, error)

Encrypt encrypts the plaintext using AES-256-GCM Returns base64-encoded ciphertext with "enc:" prefix

Example
package main

import (
	"fmt"
	"log"

	"github.com/tx7do/go-utils/crypto"
)

func main() {
	// Create an encryptor with a secret key
	encryptor, err := crypto.NewEncryptor("my-secret-key")
	if err != nil {
		log.Fatal(err)
	}

	// Encrypt sensitive data
	plaintext := `{"username":"admin","password":"secret123"}`
	encrypted, err := encryptor.Encrypt(plaintext)
	if err != nil {
		log.Fatal(err)
	}

	// Encrypted data has the "enc:" prefix
	fmt.Println("Data encrypted:", crypto.IsEncrypted(encrypted))
}
Output:
Data encrypted: true

func (*Encryptor) MustDecrypt added in v0.0.4

func (e *Encryptor) MustDecrypt(ciphertext string) string

MustDecrypt decrypts data and panics on error (useful for testing)

func (*Encryptor) MustEncrypt added in v0.0.4

func (e *Encryptor) MustEncrypt(plaintext string) string

MustEncrypt encrypts data and panics on error (useful for testing)

type HMAC

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

func NewHMAC

func NewHMAC(key []byte) *HMAC

NewHMAC 创建HMAC实例

func (*HMAC) SetKey

func (h *HMAC) SetKey(key []byte)

SetKey 变更密钥

func (*HMAC) Sum

func (h *HMAC) Sum(data []byte) string

Sum 计算HMAC-SHA256

func (*HMAC) Verify

func (h *HMAC) Verify(data []byte, expected string) bool

Verify 校验HMAC

type Hasher

type Hasher interface {
	// Sum 计算输入数据的哈希值,返回哈希结果
	Sum(data []byte) ([]byte, error)

	// Name 返回哈希算法名称
	Name() string
}

Hasher 定义了哈希算法的接口,支持计算数据的哈希值

type KeyExchanger

type KeyExchanger interface {
	// DeriveSharedSecret 计算共享密钥,用于根据对方公钥字节计算共享密钥
	DeriveSharedSecret(peerPubBytes []byte) ([]byte, error)

	// PublicKeyBytes 获取本地公钥字节
	PublicKeyBytes() []byte
}

KeyExchanger 定义了密钥协商算法的接口 适用于ECDH、SM2密钥交换等场景

type RSACipher

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

func NewRSACipher

func NewRSACipher(bits int) (*RSACipher, error)

NewRSACipher 生成新的RSA密钥对

func NewRSACipherFromKey

func NewRSACipherFromKey(priv *rsa.PrivateKey, pub *rsa.PublicKey) *RSACipher

NewRSACipherFromKey 用已有密钥初始化

func (*RSACipher) Decrypt

func (r *RSACipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt 使用RSA私钥解密

func (*RSACipher) Encrypt

func (r *RSACipher) Encrypt(plain []byte) ([]byte, error)

Encrypt 使用RSA公钥加密

func (*RSACipher) ExportPrivateKey

func (r *RSACipher) ExportPrivateKey() (string, error)

ExportPrivateKey 导出私钥PEM

func (*RSACipher) ExportPublicKey

func (r *RSACipher) ExportPublicKey() (string, error)

ExportPublicKey 导出公钥PEM

func (*RSACipher) Name

func (r *RSACipher) Name() string

func (*RSACipher) PrivateKey

func (r *RSACipher) PrivateKey() *rsa.PrivateKey

PrivateKey 获取私钥

func (*RSACipher) PublicKey

func (r *RSACipher) PublicKey() *rsa.PublicKey

PublicKey 获取公钥

type SHA256Hasher

type SHA256Hasher struct{}

func (*SHA256Hasher) Name

func (h *SHA256Hasher) Name() string

func (*SHA256Hasher) Sum

func (h *SHA256Hasher) Sum(data []byte) ([]byte, error)

type SHA512Hasher

type SHA512Hasher struct{}

func (*SHA512Hasher) Name

func (h *SHA512Hasher) Name() string

func (*SHA512Hasher) Sum

func (h *SHA512Hasher) Sum(data []byte) ([]byte, error)

type SM2Cipher

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

SM2Cipher 实现 Cipher 接口,支持国密SM2加解密 仅适用于小数据块加密(如密钥交换),不适合大数据流 生产环境请妥善管理私钥

func NewSM2Cipher

func NewSM2Cipher() (*SM2Cipher, error)

NewSM2Cipher 生成新的SM2密钥对

func NewSM2CipherFromKey

func NewSM2CipherFromKey(priv *sm2.PrivateKey, pub *sm2.PublicKey) *SM2Cipher

NewSM2CipherFromKey 用已有密钥初始化

func (*SM2Cipher) Decrypt

func (s *SM2Cipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt 使用SM2私钥解密

func (*SM2Cipher) DecryptAsn1

func (s *SM2Cipher) DecryptAsn1(ciphertext []byte) ([]byte, error)

DecryptAsn1 解密ASN.1格式密文

func (*SM2Cipher) Encrypt

func (s *SM2Cipher) Encrypt(plain []byte) ([]byte, error)

Encrypt 使用SM2公钥加密

func (*SM2Cipher) EncryptAsn1

func (s *SM2Cipher) EncryptAsn1(plain []byte) ([]byte, error)

EncryptAsn1 加密并输出ASN.1格式(更通用,兼容多数平台)

func (*SM2Cipher) Name

func (s *SM2Cipher) Name() string

Name 返回加密算法名称

func (*SM2Cipher) PrivateKey

func (s *SM2Cipher) PrivateKey() *sm2.PrivateKey

PrivateKey 获取私钥(仅用于安全存储,禁止对外泄露)

func (*SM2Cipher) PublicKey

func (s *SM2Cipher) PublicKey() *sm2.PublicKey

PublicKey 获取公钥(用于导出、传输给对方)

func (*SM2Cipher) Sign

func (s *SM2Cipher) Sign(digest []byte) (string, error)

Sign 使用SM2私钥签名(ASN.1编码)

func (*SM2Cipher) Verify

func (s *SM2Cipher) Verify(data []byte, signature string) (bool, error)

Verify 使用SM2公钥验证签名(ASN.1编码)

type SM3Hasher

type SM3Hasher struct{}

func NewSM3Hasher

func NewSM3Hasher() *SM3Hasher

NewSM3Hasher 构造SM3哈希器

func (*SM3Hasher) Name

func (h *SM3Hasher) Name() string

Name 返回哈希算法的名称

func (*SM3Hasher) Sum

func (h *SM3Hasher) Sum(data []byte) ([]byte, error)

Sum 计算数据的SM3哈希值

type SM4Cipher

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

SM4Cipher 实现国密SM4对称加密算法(ECB模式,适合小数据块)

func NewSM4Cipher

func NewSM4Cipher(key []byte) (*SM4Cipher, error)

NewSM4Cipher 创建SM4加密器

func (*SM4Cipher) Decrypt

func (s *SM4Cipher) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt ECB模式解密

func (*SM4Cipher) Encrypt

func (s *SM4Cipher) Encrypt(plain []byte) ([]byte, error)

Encrypt ECB模式加密

func (*SM4Cipher) Name

func (s *SM4Cipher) Name() string

type Signer

type Signer interface {
	// Sign 返回签名字符串
	Sign(data []byte) (string, error)

	// Name 返回算法名称
	Name() string
}

Signer 定义了签名算法的接口 适用于ECDSA、SM2等签名场景

type Verifier

type Verifier interface {
	// Verify 验证签名
	Verify(data []byte, signature string) (bool, error)

	// Name 返回算法名称
	Name() string
}

Verifier 定义了验签算法的接口 适用于ECDSA、SM2等签名场景

Jump to

Keyboard shortcuts

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