cryptoutil

package
v0.0.0-...-48a1e1e Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MulanPSL-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package cryptoutil provides utility functions for encryption, decryption, and hashing.

Index

Constants

View Source
const (
	MD5    = "md5"
	SHA1   = "sha1"
	SHA256 = "sha256"
	SHA512 = "sha512"
)

Hash algorithms

View Source
const (
	HexEncoding    = "hex"
	Base64Encoding = "base64"
)

Encoding types

Variables

This section is empty.

Functions

func AESDecrypt

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

AESDecrypt decrypts data using AES with the given key

func AESDecryptString

func AESDecryptString(encryptedData, key string) (string, error)

AESDecryptString decrypts base64 encoded string using AES

func AESEncrypt

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

AESEncrypt encrypts data using AES with the given key

func AESEncryptString

func AESEncryptString(data, key string) (string, error)

AESEncryptString encrypts string using AES

func Base64Decode

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

Base64Decode decodes base64 string to data

func Base64Encode

func Base64Encode(data []byte) string

Base64Encode encodes data to base64 string

func Base64URLDecode

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

Base64URLDecode decodes URL-safe base64 string to data

func Base64URLEncode

func Base64URLEncode(data []byte) string

Base64URLEncode encodes data to URL-safe base64 string

func DESDecrypt

func DESDecrypt(encryptedData, key []byte) ([]byte, error)

DESDecrypt decrypts data using DES with the given key (8 bytes)

func DESDecryptHex

func DESDecryptHex(encryptedHex, key string) (string, error)

DESDecryptHex decrypts hex encoded string using DES with fixed IV - compatible with your code

func DESDecryptString

func DESDecryptString(encryptedData, key string) (string, error)

DESDecryptString decrypts base64 encoded string using DES

func DESDecryptWithFixedIV

func DESDecryptWithFixedIV(encryptedData, key []byte) ([]byte, error)

DESDecryptWithFixedIV decrypts data using DES with fixed IV (key as IV) - compatible mode

func DESEncrypt

func DESEncrypt(data, key []byte) ([]byte, error)

DESEncrypt encrypts data using DES with the given key (8 bytes)

func DESEncryptHex

func DESEncryptHex(data, key string) (string, error)

DESEncryptHex encrypts string using DES with fixed IV and returns hex string - compatible with your code

func DESEncryptString

func DESEncryptString(data, key string) (string, error)

DESEncryptString encrypts string using DES

func DESEncryptWithFixedIV

func DESEncryptWithFixedIV(data, key []byte) ([]byte, error)

DESEncryptWithFixedIV encrypts data using DES with fixed IV (key as IV) - compatible mode

func GenerateRandomBytes

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

GenerateRandomBytes generates random bytes of specified length

func GenerateRandomString

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

GenerateRandomString generates random string of specified length

func HMAC

func HMAC(data, key []byte, algorithm string) (string, error)

HMAC generates HMAC using the specified hash algorithm

func HMACString

func HMACString(data, key, algorithm string) (string, error)

HMACString generates HMAC for string data

func Hash

func Hash(data []byte, algorithm string) (string, error)

Hash returns hash of the input data using the specified algorithm

func HashString

func HashString(s, algorithm string) (string, error)

HashString returns hash of the input string using the specified algorithm

func HexDecode

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

HexDecode decodes hex string to data

func HexEncode

func HexEncode(data []byte) string

HexEncode encodes data to hex string

func MD5Hash

func MD5Hash(data []byte) string

MD5Hash returns MD5 hash of the input data

func MD5String

func MD5String(s string) string

MD5String returns MD5 hash of the input string

func PKCS7Pad

func PKCS7Pad(data []byte, blockSize int) []byte

PKCS7Pad applies PKCS7 padding to data

func PKCS7Unpad

func PKCS7Unpad(data []byte) ([]byte, error)

PKCS7Unpad removes PKCS7 padding from data

func ParsePrivateKeyFromPEM

func ParsePrivateKeyFromPEM(pemData []byte) (*rsa.PrivateKey, error)

ParsePrivateKeyFromPEM parses private key from PEM format

func ParsePublicKeyFromPEM

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

ParsePublicKeyFromPEM parses public key from PEM format

func RSADecrypt

func RSADecrypt(encryptedData []byte, privateKey *rsa.PrivateKey) ([]byte, error)

RSADecrypt decrypts data using RSA private key

func RSAEncrypt

func RSAEncrypt(data []byte, publicKey *rsa.PublicKey) ([]byte, error)

RSAEncrypt encrypts data using RSA public key

func RSASign

func RSASign(data []byte, privateKey *rsa.PrivateKey) ([]byte, error)

RSASign signs data using RSA private key with SHA256

func RSAVerify

func RSAVerify(data, signature []byte, publicKey *rsa.PublicKey) error

RSAVerify verifies signature using RSA public key with SHA256

func SHA1Hash

func SHA1Hash(data []byte) string

SHA1Hash returns SHA1 hash of the input data

func SHA1String

func SHA1String(s string) string

SHA1String returns SHA1 hash of the input string

func SHA256Hash

func SHA256Hash(data []byte) string

SHA256Hash returns SHA256 hash of the input data

func SHA256String

func SHA256String(s string) string

SHA256String returns SHA256 hash of the input string

func SHA512Hash

func SHA512Hash(data []byte) string

SHA512Hash returns SHA512 hash of the input data

func SHA512String

func SHA512String(s string) string

SHA512String returns SHA512 hash of the input string

func SimpleDecrypt

func SimpleDecrypt(encryptedData, password string) (string, error)

SimpleDecrypt provides a simple decryption interface using AES-256

func SimpleEncrypt

func SimpleEncrypt(data, password string) (string, error)

SimpleEncrypt provides a simple encryption interface using AES-256

func TripleDESDecrypt

func TripleDESDecrypt(encryptedData, key []byte) ([]byte, error)

TripleDESDecrypt decrypts data using 3DES with the given key (24 bytes)

func TripleDESDecryptString

func TripleDESDecryptString(encryptedData, key string) (string, error)

TripleDESDecryptString decrypts base64 encoded string using 3DES

func TripleDESEncrypt

func TripleDESEncrypt(data, key []byte) ([]byte, error)

TripleDESEncrypt encrypts data using 3DES with the given key (24 bytes)

func TripleDESEncryptString

func TripleDESEncryptString(data, key string) (string, error)

TripleDESEncryptString encrypts string using 3DES

func VerifyHMAC

func VerifyHMAC(data, key []byte, signature, algorithm string) (bool, error)

VerifyHMAC verifies HMAC signature

func VerifyHMACString

func VerifyHMACString(data, key, signature, algorithm string) (bool, error)

VerifyHMACString verifies HMAC signature for string data

Types

type RSAKeyPair

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

RSAKeyPair represents an RSA key pair

func GenerateRSAKeyPair

func GenerateRSAKeyPair(bits int) (*RSAKeyPair, error)

GenerateRSAKeyPair generates a new RSA key pair

func (*RSAKeyPair) PrivateKeyToPEM

func (kp *RSAKeyPair) PrivateKeyToPEM() ([]byte, error)

PrivateKeyToPEM converts private key to PEM format

func (*RSAKeyPair) PublicKeyToPEM

func (kp *RSAKeyPair) PublicKeyToPEM() ([]byte, error)

PublicKeyToPEM converts public key to PEM format

Jump to

Keyboard shortcuts

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