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 ¶
- func GenerateRandomPassword(length int) (string, error)
- func GetRandomBytes(size uint) ([]byte, error)
- func GetRandomBytesBase64(size uint) (string, error)
- func Md5(v any) string
- func ReadOrSavePassphrase(file string, length int) ([]byte, error)
- func SHA256(inp []byte) string
- func StringToKey32(inp string) string
- func StringToKey32Bytes(inp string) []byte
- func XX(inp []byte) uint64
- type AesCipher
- func (a *AesCipher) Decrypt(ciphertext string, out io.Writer) *AesCipher
- func (a *AesCipher) Encrypt(plaintext string, out io.Writer) *AesCipher
- func (a *AesCipher) WithAES128() *AesCipher
- func (a *AesCipher) WithAES192() *AesCipher
- func (a *AesCipher) WithAES256() *AesCipher
- func (a *AesCipher) WithPassphrase(passphrase []byte) *AesCipher
- type PGPCipher
- func (p *PGPCipher) Decrypt(ciphertext []byte, out io.Writer) *PGPCipher
- func (p *PGPCipher) DecryptWithPassword(ciphertext []byte, out io.Writer) *PGPCipher
- func (p *PGPCipher) DecryptWithPrivateKey(ciphertext []byte, out io.Writer) *PGPCipher
- func (p *PGPCipher) Encrypt(plaintext []byte, out io.Writer) *PGPCipher
- func (p *PGPCipher) EncryptWithPassword(plaintext []byte, out io.Writer) *PGPCipher
- func (p *PGPCipher) EncryptWithPublicKey(plaintext []byte, out io.Writer) *PGPCipher
- func (p *PGPCipher) GenerateKeyPair(name, email string) error
- func (p *PGPCipher) GetPrivateKey() (string, error)
- func (p *PGPCipher) GetPublicKey() (string, error)
- func (p *PGPCipher) WithPassphrase(passphrase []byte) *PGPCipher
- func (p *PGPCipher) WithPrivateKey(key string) *PGPCipher
- func (p *PGPCipher) WithPrivateKeyFromFile(filePath string) *PGPCipher
- func (p *PGPCipher) WithPublicKey(key string) *PGPCipher
- func (p *PGPCipher) WithPublicKeyFromFile(filePath string) *PGPCipher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateRandomPassword ¶
GenerateRandomPassword generates a random password of the specified length
func GetRandomBytes ¶
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 ¶
GetRandomBytesBase64 Returns the Base64 Encoded Equivalent of calling GetRandomBytes.
func ReadOrSavePassphrase ¶
ReadOrSavePassphrase generates a random passphrase of the specified length and saves it if the file does not exist.
func StringToKey32 ¶
StringToKey32 produces a 32byte slice from the input string using SHA256
func StringToKey32Bytes ¶
StringToKey32Bytes produces a 32byte slice from the input string using SHA256
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 ¶
Decrypt uses the specified symmetric key to decrypt the input string using AES
func (*AesCipher) Encrypt ¶
Encrypt uses the specified symmetric key to encrypt the input string using AES
func (*AesCipher) WithAES128 ¶
WithAES128 sets the key size to 128 bits (16 bytes).
func (*AesCipher) WithAES192 ¶
WithAES192 sets the key size to 192 bits (24 bytes).
func (*AesCipher) WithAES256 ¶
WithAES256 sets the key size to 256 bits (32 bytes).
func (*AesCipher) WithPassphrase ¶
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 ¶
NewPGPCipher creates a new PGPCipher instance with the default PGP profile (RFC4880).
func (*PGPCipher) Decrypt ¶
Decrypt decrypts the given ciphertext using the provided private key or passphrase.
func (*PGPCipher) DecryptWithPassword ¶
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 ¶
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 ¶
Encrypt encrypts the given plaintext using the provided public key or passphrase.
func (*PGPCipher) EncryptWithPassword ¶
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 ¶
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 ¶
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 ¶
GetPrivateKey returns the private key set for the PGPCipher instance.
func (*PGPCipher) GetPublicKey ¶
GetPublicKey returns the public key set for the PGPCipher instance or an error if no public key is set.
func (*PGPCipher) WithPassphrase ¶
WithPassphrase sets the passphrase for the PGPCipher instance.
func (*PGPCipher) WithPrivateKey ¶
WithPrivateKey sets the private key for the PGPCipher instance.
func (*PGPCipher) WithPrivateKeyFromFile ¶
WithPrivateKeyFromFile sets the private key for the PGPCipher instance from a file.
func (*PGPCipher) WithPublicKey ¶
WithPublicKey sets the public key for the PGPCipher instance.
func (*PGPCipher) WithPublicKeyFromFile ¶
WithPublicKeyFromFile sets the public key for the PGPCipher instance from a file.