cryptutils

package
v0.0.0-...-98f1bff Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2025 License: Unlicense Imports: 12 Imported by: 0

Documentation

Overview

Package cryptutils 提供加密解密相关的工具函数

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AESDecrypt

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

AESDecrypt 使用AES-GCM模式解密数据 参数:

  • ciphertext: 要解密的密文数据 (包含nonce)
  • key: 解密密钥 (必须是16, 24或32字节长,对应AES-128, AES-192或AES-256)

返回:

  • []byte: 解密后的明文数据
  • error: 如果解密失败则返回错误信息

func AESEncrypt

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

AESEncrypt 使用AES-GCM模式加密数据 参数:

  • plaintext: 要加密的明文数据
  • key: 加密密钥 (必须是16, 24或32字节长,对应AES-128, AES-192或AES-256)

返回:

  • []byte: 加密后的数据 (包含随机生成的nonce)
  • error: 如果加密失败则返回错误信息

func Base64Decode

func Base64Decode(encoded string) ([]byte, error)

Base64Decode 解码Base64字符串 参数:

  • encoded: Base64编码的字符串

返回:

  • []byte: 解码后的数据
  • error: 如果解码失败则返回错误信息

func Base64Encode

func Base64Encode(data []byte) string

Base64Encode 将数据编码为Base64字符串 参数:

  • data: 要编码的数据

返回:

  • string: Base64编码的字符串

func GenerateRandomBytes

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

GenerateRandomBytes 生成指定长度的随机字节 参数:

  • length: 要生成的随机字节长度

返回:

  • []byte: 生成的随机字节
  • error: 如果生成失败则返回错误信息

func GenerateRandomHex

func GenerateRandomHex(length int) (string, error)

GenerateRandomHex 生成指定长度的随机十六进制字符串 参数:

  • length: 要生成的十六进制字符串长度(字节数的两倍)

返回:

  • string: 生成的十六进制字符串
  • error: 如果生成失败则返回错误信息

func GenerateRandomString

func GenerateRandomString(length int, charset string) (string, error)

GenerateRandomString 生成指定长度的随机字符串 参数:

  • length: 要生成的字符串长度
  • charset: 字符集(如果为空,则使用默认字符集)

返回:

  • string: 生成的随机字符串
  • error: 如果生成失败则返回错误信息

func GenerateUUID

func GenerateUUID() (string, error)

GenerateUUID 生成一个随机的UUID (版本4) 返回:

  • string: 生成的UUID字符串
  • error: 如果生成失败则返回错误信息

func GenerateUUIDWithoutHyphens

func GenerateUUIDWithoutHyphens() (string, error)

GenerateUUIDWithoutHyphens 生成一个没有连字符的UUID 返回:

  • string: 生成的UUID字符串(无连字符)
  • error: 如果生成失败则返回错误信息

func MD5Hash

func MD5Hash(data []byte) string

MD5Hash 计算字符串的MD5哈希值 参数:

  • data: 要计算哈希的数据

返回:

  • string: 十六进制格式的MD5哈希值

func SHA1Hash

func SHA1Hash(data []byte) string

SHA1Hash 计算字符串的SHA1哈希值 参数:

  • data: 要计算哈希的数据

返回:

  • string: 十六进制格式的SHA1哈希值

func SHA256Hash

func SHA256Hash(data []byte) string

SHA256Hash 计算字符串的SHA256哈希值 参数:

  • data: 要计算哈希的数据

返回:

  • string: 十六进制格式的SHA256哈希值

func SHA512Hash

func SHA512Hash(data []byte) string

SHA512Hash 计算字符串的SHA512哈希值 参数:

  • data: 要计算哈希的数据

返回:

  • string: 十六进制格式的SHA512哈希值

Types

This section is empty.

Jump to

Keyboard shortcuts

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