encryption

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstantTimeEqual

func ConstantTimeEqual(a, b []byte) bool

ConstantTimeEqual performs constant-time comparison of two byte slices.

func GenerateRandomBytes

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

GenerateRandomBytes generates cryptographically secure random bytes.

func GenerateRandomKey

func GenerateRandomKey(length int) string

GenerateRandomKey generates a cryptographically secure random key.

Types

type Encrypter

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

Encrypter manages data encryption and decryption.

func NewEncrypter

func NewEncrypter(appKey string) (*Encrypter, error)

NewEncrypter creates a new encrypter instance. Keys are hashed with SHA-256 to produce a consistent 32-byte key, preventing collision from zero-padding short keys.

func (*Encrypter) Decrypt

func (e *Encrypter) Decrypt(payload string) (string, error)

Decrypt decrypts a previously encrypted string.

func (*Encrypter) Encrypt

func (e *Encrypter) Encrypt(value string) (string, error)

Encrypt encrypts a plaintext string using AES-256-GCM.

type SignedMessage

type SignedMessage struct {
	Payload   string `json:"payload"`
	Signature string `json:"signature"`
	Timestamp int64  `json:"timestamp"`
}

SignedMessage represents a signed message with timestamp.

type SignedURL

type SignedURL struct {
	URL       string
	Signature string
}

SignedURL represents a URL with a signature.

type Signer

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

Signer provides message signing and verification.

func NewSigner

func NewSigner(secret string) (*Signer, error)

NewSigner creates a new Signer with the given secret key.

func (*Signer) Hash

func (s *Signer) Hash(message string) string

Hash creates a SHA-256 hash of the message (one-way).

func (*Signer) HashHMAC

func (s *Signer) HashHMAC(message string) string

HashHMAC creates an HMAC-SHA256 hash (keyed, one-way).

func (*Signer) Sign

func (s *Signer) Sign(message string) string

Sign signs a message using HMAC-SHA256.

func (*Signer) SignHMAC

func (s *Signer) SignHMAC(message string) string

SignHMAC signs a message using HMAC-SHA512.

func (*Signer) SignURL

func (s *Signer) SignURL(url string) *SignedURL

SignURL signs a URL path.

func (*Signer) SignWithTimestamp

func (s *Signer) SignWithTimestamp(message string, timestamp int64) *SignedMessage

SignWithTimestamp signs a message with a timestamp.

func (*Signer) Verify

func (s *Signer) Verify(message, signature string) bool

Verify verifies a message signature using HMAC-SHA256.

func (*Signer) VerifyHMAC

func (s *Signer) VerifyHMAC(message, signature string) bool

VerifyHMAC verifies a message signature using HMAC-SHA512.

func (*Signer) VerifySignedMessage

func (s *Signer) VerifySignedMessage(sm *SignedMessage, maxAge int64) bool

VerifySignedMessage verifies a signed message with timestamp.

func (*Signer) VerifyURL

func (s *Signer) VerifyURL(url, signature string) bool

VerifyURL verifies a signed URL.

Jump to

Keyboard shortcuts

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