security

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2025 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package security provides cryptographic utilities and PGP encryption/decryption functionality.

It offers functions for generating cryptographically secure random bytes and passwords, hashing data using SHA256, MD5, and xxHash algorithms, and deriving keys from strings.

The package also includes the PGPCipher struct, which leverages the gopenpgp library to perform PGP encryption and decryption with both public/private key pairs and passphrase-based methods.

Key features:

  • Secure random byte generation and base64 encoding
  • Password generation with customizable character sets
  • Hashing functions: SHA256, MD5, and xxHash
  • Reading and saving passphrases securely to files
  • PGP encryption/decryption with support for:
  • Public key encryption
  • Private key decryption
  • Passphrase-based encryption and decryption
  • Key pair generation with user identity and high security settings
  • Convenient methods to load keys from files or strings

This package is designed for use cases requiring cryptographically secure operations and OpenPGP-compatible message encryption in Go applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRandomPassword

func GenerateRandomPassword(length int) (string, error)

GenerateRandomPassword generates a random password of the specified length

func GetRandomBytes

func GetRandomBytes(size uint) ([]byte, error)

GetRandomBytes Generates random Bytes using crypto/rand which is significantly slower than math/rand but cryptographically secure. This Should be used whenever cryptographic keys should be generated.

func GetRandomBytesBase64

func GetRandomBytesBase64(size uint) (string, error)

GetRandomBytesBase64 Returns the Base64 Encoded Equivalent of calling GetRandomBytes.

func Md5

func Md5(v any) string

Md5 returns the MD5 hash of the input as a hex string

func ReadOrSavePassphrase

func ReadOrSavePassphrase(file string, length int) ([]byte, error)

ReadOrSavePassphrase generates a random passphrase of the specified length and saves it if the file does not exist.

func SHA256

func SHA256(inp []byte) string

SHA256 returns the SHA256 hash of the input as a hex string

func StringToKey32

func StringToKey32(inp string) string

StringToKey32 produces a 32byte slice from the input string using SHA256

func StringToKey32Bytes

func StringToKey32Bytes(inp string) []byte

StringToKey32Bytes produces a 32byte slice from the input string using SHA256

func XX

func XX(inp []byte) uint64

XX returns the xxhash hash of the input as a uint64

Types

type AesCipher

type AesCipher struct {
	Error error
	// contains filtered or unexported fields
}

AesCipher is a struct that provides methods for encrypting and decrypting data using AES. It uses the crypto/aes and crypto/cipher packages from the Go standard library.

func NewAesCipher

func NewAesCipher() *AesCipher

NewAesCipher creates a new AesCipher instance with the specified key.

func (*AesCipher) Decrypt

func (a *AesCipher) Decrypt(ciphertext string, out io.Writer) *AesCipher

Decrypt uses the specified symmetric key to decrypt the input string using AES

func (*AesCipher) Encrypt

func (a *AesCipher) Encrypt(plaintext string, out io.Writer) *AesCipher

Encrypt uses the specified symmetric key to encrypt the input string using AES

func (*AesCipher) WithAES128

func (a *AesCipher) WithAES128() *AesCipher

WithAES128 sets the key size to 128 bits (16 bytes).

func (*AesCipher) WithAES192

func (a *AesCipher) WithAES192() *AesCipher

WithAES192 sets the key size to 192 bits (24 bytes).

func (*AesCipher) WithAES256

func (a *AesCipher) WithAES256() *AesCipher

WithAES256 sets the key size to 256 bits (32 bytes).

func (*AesCipher) WithPassphrase

func (a *AesCipher) WithPassphrase(passphrase []byte) *AesCipher

WithPassphrase sets the key to the specified byte slice. The key must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

type PGPCipher

type PGPCipher struct {
	Error error
	// contains filtered or unexported fields
}

PGPCipher is a struct that provides methods for encrypting and decrypting data using PGP. It uses the gopenpgp library for PGP operations and supports both public key and passphrase-based encryption.

func NewPGPCipher

func NewPGPCipher(p *profile.Custom) *PGPCipher

NewPGPCipher creates a new PGPCipher instance with the default PGP profile (RFC4880).

func (*PGPCipher) Decrypt

func (p *PGPCipher) Decrypt(ciphertext []byte, out io.Writer) *PGPCipher

Decrypt decrypts the given ciphertext using the provided private key or passphrase.

func (*PGPCipher) DecryptWithPassword

func (p *PGPCipher) DecryptWithPassword(ciphertext []byte, out io.Writer) *PGPCipher

DecryptWithPassword decrypts the given ciphertext using the provided passphrase. The passphrase must be set using the WithPassphrase method before calling this function.

func (*PGPCipher) DecryptWithPrivateKey

func (p *PGPCipher) DecryptWithPrivateKey(ciphertext []byte, out io.Writer) *PGPCipher

DecryptWithPrivateKey decrypts the given ciphertext using the provided private key. The private key must be set using the WithPrivateKey method before calling this function. When the private key is encrypted, the passphrase must also be set using the WithPassphrase method.

func (*PGPCipher) Encrypt

func (p *PGPCipher) Encrypt(plaintext []byte, out io.Writer) *PGPCipher

Encrypt encrypts the given plaintext using the provided public key or passphrase.

func (*PGPCipher) EncryptWithPassword

func (p *PGPCipher) EncryptWithPassword(plaintext []byte, out io.Writer) *PGPCipher

EncryptWithPassword encrypts the given plaintext using the provided passphrase. The passphrase must be set using the WithPassphrase method before calling this function.

func (*PGPCipher) EncryptWithPublicKey

func (p *PGPCipher) EncryptWithPublicKey(plaintext []byte, out io.Writer) *PGPCipher

EncryptWithPublicKey encrypts the given plaintext using the provided public key. The public key must be set using the WithPublicKey method before calling this function.

func (*PGPCipher) GenerateKeyPair

func (p *PGPCipher) GenerateKeyPair(name, email string) error

GenerateKeyPair generates a new key pair with the given name and email. To encrypt the private key, a passphrase must be set using the WithPassphrase method.

func (*PGPCipher) GetPrivateKey

func (p *PGPCipher) GetPrivateKey() (string, error)

GetPrivateKey returns the private key set for the PGPCipher instance.

func (*PGPCipher) GetPublicKey

func (p *PGPCipher) GetPublicKey() (string, error)

GetPublicKey returns the public key set for the PGPCipher instance or an error if no public key is set.

func (*PGPCipher) WithPassphrase

func (p *PGPCipher) WithPassphrase(passphrase []byte) *PGPCipher

WithPassphrase sets the passphrase for the PGPCipher instance.

func (*PGPCipher) WithPrivateKey

func (p *PGPCipher) WithPrivateKey(key string) *PGPCipher

WithPrivateKey sets the private key for the PGPCipher instance.

func (*PGPCipher) WithPrivateKeyFromFile

func (p *PGPCipher) WithPrivateKeyFromFile(filePath string) *PGPCipher

WithPrivateKeyFromFile sets the private key for the PGPCipher instance from a file.

func (*PGPCipher) WithPublicKey

func (p *PGPCipher) WithPublicKey(key string) *PGPCipher

WithPublicKey sets the public key for the PGPCipher instance.

func (*PGPCipher) WithPublicKeyFromFile

func (p *PGPCipher) WithPublicKeyFromFile(filePath string) *PGPCipher

WithPublicKeyFromFile sets the public key for the PGPCipher instance from a file.

Jump to

Keyboard shortcuts

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