encrypt

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

encrypt/aes.go

Package encrypt 提供加密相关的工具函数,包括哈希、对称加密和非对称加密。

encrypt 包提供一组简单的函数式 API,涵盖常用的加密操作。

功能特性

  • 哈希函数:支持 MD5、SHA1、SHA256、SHA512 和 Blake3
  • 对称加密:AES-GCM 模式(AES-128/192/256)
  • 非对称加密:RSA OAEP 和 PKCS1v15
  • 编码工具:Base64 和 Hex 编码/解码
  • 线程安全:所有函数可在多个 goroutine 中并发使用

快速开始

哈希计算:

package main

import (
	"fmt"
	"github.com/f2xme/gox/encrypt"
)

func main() {
	// SHA256 哈希
	hash := encrypt.SHA256([]byte("hello"))
	fmt.Println(hash)
	// 输出: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

	// Blake3 哈希(更快更安全)
	hash = encrypt.Blake3([]byte("hello"))
	fmt.Println(hash)
}

AES 对称加密:

package main

import (
	"crypto/rand"
	"fmt"
	"github.com/f2xme/gox/encrypt"
)

func main() {
	// 生成 AES-256 密钥
	key := make([]byte, 32)
	rand.Read(key)

	// 加密
	plaintext := []byte("secret message")
	ciphertext, err := encrypt.AESEncrypt(plaintext, key)
	if err != nil {
		panic(err)
	}

	// 解密
	decrypted, err := encrypt.AESDecrypt(ciphertext, key)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(decrypted)) // 输出: secret message
}

RSA 非对称加密:

package main

import (
	"fmt"
	"github.com/f2xme/gox/encrypt"
)

func main() {
	// 生成 RSA 密钥对
	pubKey, privKey, err := encrypt.GenerateRSAKeyPair(2048)
	if err != nil {
		panic(err)
	}

	// 加密
	plaintext := []byte("secret")
	ciphertext, err := encrypt.RSAEncrypt(plaintext, pubKey)
	if err != nil {
		panic(err)
	}

	// 解密
	decrypted, err := encrypt.RSADecrypt(ciphertext, privKey)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(decrypted)) // 输出: secret
}

编码工具

Base64 和 Hex 编码可与加密函数自由组合:

// Base64 编码密文
encoded := encrypt.EncodeBase64(ciphertext)

// Base64 解码
decoded, err := encrypt.DecodeBase64(encoded)

// Hex 编码
hexStr := encrypt.EncodeHex(data)

// Hex 解码
data, err := encrypt.DecodeHex(hexStr)

最佳实践

选择合适的哈希算法:

// 推荐:SHA256 或 Blake3
hash := encrypt.SHA256(data)
hash := encrypt.Blake3(data)

// 不推荐:MD5 或 SHA1(已不安全,仅用于兼容性)
hash := encrypt.MD5(data)

使用足够长的密钥:

// AES-256(推荐)
key := make([]byte, 32)
rand.Read(key)

// RSA-2048 或更高
pubKey, privKey, _ := encrypt.GenerateRSAKeyPair(2048)

安全存储密钥:

// 不要硬编码密钥
// 使用环境变量或密钥管理服务
key := []byte(os.Getenv("ENCRYPTION_KEY"))

使用 OAEP 而非 PKCS1v15:

// 推荐:OAEP(更安全)
ciphertext, _ := encrypt.RSAEncrypt(data, pubKey)

// 不推荐:PKCS1v15(已过时)
ciphertext, _ := encrypt.RSAEncryptPKCS1v15(data, pubKey)

注意事项

  • MD5 和 SHA1 已在密码学上被破解,仅用于兼容旧系统
  • RSA PKCS1v15 已过时,新应用应使用 OAEP
  • AES 密钥必须是 16、24 或 32 字节(对应 AES-128/192/256)
  • RSA 密钥建议至少 2048 位
  • 所有函数都是线程安全的

encrypt/encoding.go

encrypt/hash.go

encrypt/rsa.go

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrDecryptionFailed = errors.New("encrypt: decryption failed")

ErrDecryptionFailed 当解密因认证或填充错误失败时返回

View Source
var ErrInvalidCiphertext = errors.New("encrypt: invalid ciphertext")

ErrInvalidCiphertext 当密文格式错误或太短时返回

View Source
var ErrInvalidKeySize = errors.New("encrypt: invalid key size")

ErrInvalidKeySize 当密钥大小对算法无效时返回

View Source
var ErrInvalidKeyType = errors.New("encrypt: invalid key type")

ErrInvalidKeyType 当解析的密钥不是预期类型时返回

View Source
var ErrInvalidPEM = errors.New("encrypt: invalid PEM format")

ErrInvalidPEM 当 PEM 数据无法解码时返回

Functions

func AESDecrypt

func AESDecrypt(ciphertext, key []byte) ([]byte, error)

AESDecrypt 使用 AES-GCM 和给定密钥解密密文 密钥必须是 16、24 或 32 字节,分别对应 AES-128、AES-192 或 AES-256 密文必须是 AESEncrypt 生成的格式

func AESEncrypt

func AESEncrypt(plaintext, key []byte) ([]byte, error)

AESEncrypt 使用 AES-GCM 和给定密钥加密明文 密钥必须是 16、24 或 32 字节,分别对应 AES-128、AES-192 或 AES-256 返回的密文格式:[nonce(12) || encrypted_data || auth_tag(16)]

Example
package main

import (
	"fmt"

	"github.com/f2xme/gox/encrypt"
)

func main() {
	// 32 字节密钥 = AES-256
	key := []byte("12345678901234567890123456789012")
	plaintext := []byte("secret message")

	ciphertext, err := encrypt.AESEncrypt(plaintext, key)
	if err != nil {
		fmt.Println("encrypt error:", err)
		return
	}

	decrypted, err := encrypt.AESDecrypt(ciphertext, key)
	if err != nil {
		fmt.Println("decrypt error:", err)
		return
	}

	fmt.Println(string(decrypted))
}
Output:
secret message

func Blake3

func Blake3(data []byte) string

Blake3 计算数据的 BLAKE3 哈希值并返回小写十六进制字符串 Blake3 比 MD5/SHA1 更快更安全,推荐用于新应用

Example
package main

import (
	"fmt"

	"github.com/f2xme/gox/encrypt"
)

func main() {
	hash := encrypt.Blake3([]byte("123456"))
	fmt.Println(hash)
}
Output:
7adb787627ad5ee341fa0ba46a956e78fd85c39e195119bb260d5181b4f1e4ba

func DecodeBase64

func DecodeBase64(s string) ([]byte, error)

DecodeBase64 解码标准 Base64 字符串

func DecodeHex

func DecodeHex(s string) ([]byte, error)

DecodeHex 解码十六进制字符串,接受大写和小写

func EncodeBase64

func EncodeBase64(data []byte) string

EncodeBase64 将数据编码为标准 Base64 字符串

Example
package main

import (
	"fmt"

	"github.com/f2xme/gox/encrypt"
)

func main() {
	// 加密后使用 Base64 编码便于传输
	key := []byte("12345678901234567890123456789012")
	ciphertext, _ := encrypt.AESEncrypt([]byte("secret"), key)

	encoded := encrypt.EncodeBase64(ciphertext)
	fmt.Println("base64 length > 0:", len(encoded) > 0)

	decoded, _ := encrypt.DecodeBase64(encoded)
	plaintext, _ := encrypt.AESDecrypt(decoded, key)
	fmt.Println(string(plaintext))
}
Output:
base64 length > 0: true
secret

func EncodeHex

func EncodeHex(data []byte) string

EncodeHex 将数据编码为小写十六进制字符串

func GenerateRSAKeyPair

func GenerateRSAKeyPair(bits int) (publicKeyPEM, privateKeyPEM string, err error)

GenerateRSAKeyPair 生成 RSA 密钥对并返回 PEM 编码的公钥和私钥

func MD5

func MD5(data []byte) string

MD5 计算数据的 MD5 哈希值并返回小写十六进制字符串

已弃用:MD5 在密码学上已被破解。新应用请使用 SHA256 或 Blake3

Example
package main

import (
	"fmt"

	"github.com/f2xme/gox/encrypt"
)

func main() {
	hash := encrypt.MD5([]byte("123456"))
	fmt.Println(hash)
}
Output:
e10adc3949ba59abbe56e057f20f883e

func RSADecrypt

func RSADecrypt(ciphertext []byte, privateKeyPEM string) ([]byte, error)

RSADecrypt 使用 RSA-OAEP 和 SHA256 解密密文 privateKeyPEM 必须是 PEM 格式

func RSADecryptPKCS1v15

func RSADecryptPKCS1v15(ciphertext []byte, privateKeyPEM string) ([]byte, error)

RSADecryptPKCS1v15 使用 RSA PKCS1v15 解密密文

已弃用:PKCS1v15 不如 OAEP 安全。新应用请使用 RSADecrypt

func RSAEncrypt

func RSAEncrypt(plaintext []byte, publicKeyPEM string) ([]byte, error)

RSAEncrypt 使用 RSA-OAEP 和 SHA256 加密明文 publicKeyPEM 必须是 PEM 格式

Example
package main

import (
	"fmt"

	"github.com/f2xme/gox/encrypt"
)

func main() {
	pubKey, privKey, err := encrypt.GenerateRSAKeyPair(2048)
	if err != nil {
		fmt.Println("keygen error:", err)
		return
	}

	plaintext := []byte("hello RSA")
	ciphertext, err := encrypt.RSAEncrypt(plaintext, pubKey)
	if err != nil {
		fmt.Println("encrypt error:", err)
		return
	}

	decrypted, err := encrypt.RSADecrypt(ciphertext, privKey)
	if err != nil {
		fmt.Println("decrypt error:", err)
		return
	}

	fmt.Println(string(decrypted))
}
Output:
hello RSA

func RSAEncryptPKCS1v15

func RSAEncryptPKCS1v15(plaintext []byte, publicKeyPEM string) ([]byte, error)

RSAEncryptPKCS1v15 使用 RSA PKCS1v15 加密明文

已弃用:PKCS1v15 不如 OAEP 安全。新应用请使用 RSAEncrypt

func SHA1

func SHA1(data []byte) string

SHA1 计算数据的 SHA-1 哈希值并返回小写十六进制字符串

已弃用:SHA1 在密码学上较弱。新应用请使用 SHA256 或 Blake3

func SHA256

func SHA256(data []byte) string

SHA256 计算数据的 SHA-256 哈希值并返回小写十六进制字符串

Example
package main

import (
	"fmt"

	"github.com/f2xme/gox/encrypt"
)

func main() {
	hash := encrypt.SHA256([]byte("hello"))
	fmt.Println(hash)
}
Output:
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

func SHA512

func SHA512(data []byte) string

SHA512 计算数据的 SHA-512 哈希值并返回小写十六进制字符串

Types

This section is empty.

Jump to

Keyboard shortcuts

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