ioeither

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// OpenSSLEncryption returns the encryption environment using OpenSSL
	OpenSSLEncryption = IO.MakeIO(func() Encryption {
		return Encryption{
			EncryptBasic:       OpenSSLEncryptBasic,
			CertFingerprint:    OpenSSLCertFingerprint,
			PrivKeyFingerprint: OpenSSLPrivKeyFingerprint,
			PrivKey:            OpenSSLPrivateKey,
			PubKey:             OpenSSLPublicKey,
			SignDigest:         OpenSSLSignDigest,
		}
	})

	// CryptoEncryption returns the encryption environment using golang crypto
	CryptoEncryption = IO.MakeIO(func() Encryption {
		return Encryption{
			EncryptBasic:       CryptoEncryptBasic,
			CertFingerprint:    CryptoCertFingerprint,
			PrivKeyFingerprint: CryptoPrivKeyFingerprint,
			PrivKey:            CryptoPrivateKey,
			PubKey:             CryptoPublicKey,
			SignDigest:         CryptoSignDigest,
		}
	})

	// DefaultEncryption detects the encryption environment
	DefaultEncryption = F.Pipe1(
		validOpenSSL,
		IOE.Fold(F.Constant1[error](CryptoEncryption), F.Constant1[string](OpenSSLEncryption)),
	)
)
View Source
var (

	// CryptoCertFingerprint computes the fingerprint of a certificate using the crypto library
	CryptoCertFingerprint = F.Flow5(
		pemDecodeFirstCertificate,
		E.Chain(parseCertificateE),
		E.Map[error](rawFromCertificate),
		E.Map[error](sha256.Sum256),
		E.Map[error](shaToBytes),
	)

	// CryptoPrivKeyFingerprint computes the fingerprint of a private key using the crypto library
	CryptoPrivKeyFingerprint = F.Flow7(
		pemDecodeE,
		E.Chain(parsePrivateKeyE),
		E.Map[error](privToPub),
		E.Map[error](pubToAny),
		E.Chain(marshalPKIXPublicKeyE),
		E.Map[error](sha256.Sum256),
		E.Map[error](shaToBytes),
	)

	// CryptoVerifyDigest verifies the signature of the input data against a signature
	CryptoVerifyDigest = F.Flow2(
		pubToRsaKey,
		E.Fold(errorValidator, verifyPKCS1v15),
	)

	// CryptoPublicKey extracts the public key from a private key
	CryptoPublicKey = F.Flow6(
		pemDecodeE,
		E.Chain(parsePrivateKeyE),
		E.Map[error](privToPub),
		E.Map[error](pubToAny),
		E.Chain(marshalPKIXPublicKeyE),
		E.Map[error](func(data []byte) []byte {
			return pem.EncodeToMemory(
				&pem.Block{
					Type:  EC.TypePublicKey,
					Bytes: data,
				},
			)
		}),
	)

	// IsPublicKey checks if a PEM block is a public key
	IsPublicKey = EC.IsType(EC.TypePublicKey)

	// IsCertificate checks if a PEM block is a certificate
	IsCertificate = EC.IsType(EC.TypeCertificate)

	// CryptoAsymmetricEncryptPubOrCert encrypts a piece of text using a public key or a certificate
	CryptoAsymmetricEncryptPubOrCert = cryptoAsymmetricEncrypt(pubOrCertToRsaKey)

	// CryptoAsymmetricEncryptPub encrypts a piece of text using a public key
	CryptoAsymmetricEncryptPub = cryptoAsymmetricEncrypt(pubToRsaKey)

	// CryptoAsymmetricEncryptCert encrypts a piece of text using a certificate
	CryptoAsymmetricEncryptCert = cryptoAsymmetricEncrypt(certToRsaKey)
)
View Source
var (

	// OpenSSLSignDigest signs the sha256 digest using a private key
	OpenSSLSignDigest = handle(signDigest)

	// AsymmetricEncryptPubOrCert implements asymmetric encryption based on a public key or certificate based on the input
	AsymmetricEncryptPubOrCert = handle(asymmetricEncryptPubOrCert)

	// AsymmetricEncryptPub implements asymmetric encryption based on a public key
	AsymmetricEncryptPub = handle(asymmetricEncryptPub)

	// AsymmetricEncryptCert implements asymmetric encryption based on a certificate
	AsymmetricEncryptCert = handle(asymmetricEncryptCert)

	AsymmerticDecrypt = handle(asymmetricDecrypt)

	SymmetricEncrypt = handle(symmetricEncrypt)

	// CertSerial gets the serial number from a certificate
	CertSerial = F.Flow2(
		OpenSSL("x509", "-serial", "-noout"),
		mapStdout,
	)

	// OpenSSLPrivateKey generates a private key
	OpenSSLPrivateKey = F.Pipe2(
		emptyBytes,
		OpenSSL("genrsa", "4096"),
		mapStdout,
	)
)
View Source
var CryptoPrivateKey = F.Pipe1(
	IOE.TryCatchError(func() (*rsa.PrivateKey, error) {
		return rsa.GenerateKey(rand.Reader, 4096)
	}),
	IOE.Map[error](privKeyToPem),
)

CryptoPrivateKey generates a private key

Functions

func CryptoEncryptBasic

func CryptoEncryptBasic(pubKeyOrCert []byte) func([]byte) IOE.IOEither[error, string]

CryptoEncryptBasic implements basic encryption using golang crypto libraries given the public key or certificate

func CryptoRandomPassword

func CryptoRandomPassword(count int) IOE.IOEither[error, []byte]

CryptoRandomPassword creates a random password of given length using characters from the base64 alphabet only

func CryptoSignDigest

func CryptoSignDigest(privKey []byte) func(data []byte) IOE.IOEither[error, []byte]

CryptoSignDigest generates a signature across the sha256 of the message privkey - the private key used to compute the signature data - the message to be signed

func CryptoSymmetricEncrypt

func CryptoSymmetricEncrypt(srcPlainbBytes []byte) func([]byte) IOE.IOEither[error, string]

CryptoSymmetricEncrypt encrypts a set of bytes using a password

func DecryptBasic

func DecryptBasic(
	asymmDecrypt func(string) IOE.IOEither[error, []byte],
	symmDecrypt func(string) func([]byte) IOE.IOEither[error, []byte],
) func(string) IOE.IOEither[error, []byte]

DecryptBasic implements the basic decryption operations

func EncryptBasic

func EncryptBasic(
	genPwd IOE.IOEither[error, []byte],
	asymmEncrypt func([]byte) IOE.IOEither[error, string],
	symmEncrypt EncryptBasicFunc,
) func([]byte) IOE.IOEither[error, string]

EncryptBasic implements the basic encryption operations

func OpenSSLCertFingerprint

func OpenSSLCertFingerprint(cert []byte) E.Either[error, []byte]

func OpenSSLDecryptBasic

func OpenSSLDecryptBasic(privKey []byte) func(string) IOE.IOEither[error, []byte]

OpenSSLDecryptBasic implements basic decryption using openSSL given the private key

func OpenSSLEncryptBasic

func OpenSSLEncryptBasic(pubOrCert []byte) func([]byte) IOE.IOEither[error, string]

OpenSSLEncryptBasic implements basic encryption using openSSL given the certificate or public key

func OpenSSLPrivKeyFingerprint

func OpenSSLPrivKeyFingerprint(privKey []byte) E.Either[error, []byte]

func OpenSSLPublicKey

func OpenSSLPublicKey(privKey []byte) E.Either[error, []byte]

func OpenSSLPublicKeyFromCertificate

func OpenSSLPublicKeyFromCertificate(certificate []byte) E.Either[error, []byte]

func OpenSSLRandomPassword

func OpenSSLRandomPassword(count int) IOE.IOEither[error, []byte]

OpenSSLRandomPassword creates a random password of given length using characters from the base64 alphabet only

func OpenSSLVerifyDigest

func OpenSSLVerifyDigest(pubKey []byte) func(data []byte) func(signature []byte) IOO.IOOption[error]

OpenSSLVerifyDigest verifies the signature of the input data against a signature

func SignatureTest

func SignatureTest(
	privateKey IOE.IOEither[error, []byte],
	pubKey func([]byte) E.Either[error, []byte],
	randomData IOE.IOEither[error, []byte],
	signer func([]byte) func([]byte) IOE.IOEither[error, []byte],
	validator func([]byte) func([]byte) func([]byte) IOO.IOOption[error],
) func(t *testing.T)

func SymmetricDecrypt

func SymmetricDecrypt(token string) func([]byte) IOE.IOEither[error, []byte]

Types

type CertFingerprintFunc

type CertFingerprintFunc = func([]byte) E.Either[error, []byte]

type EncryptBasicFunc

type EncryptBasicFunc = func([]byte) func([]byte) IOE.IOEither[error, string]

type Encryption

type Encryption struct {
	// EncryptBasic implements basic encryption given the certificate (side effect because of random passphrase)
	EncryptBasic EncryptBasicFunc
	// CertFingerprint computes the fingerprint of a certificate
	CertFingerprint CertFingerprintFunc
	// PrivKeyFingerprint computes the fingerprint of a private key
	PrivKeyFingerprint PrivKeyFingerprintFunc
	// PrivKey computes a new private key
	PrivKey Key
	// PubKey computes a public key from a private key
	PubKey PubKeyFunc
	// SignDigest computes the sha256 signature using a private key (side effect because of RSA blinding)
	SignDigest SignDigestFunc
}

Encryption captures the crypto functions required to implement the source providers

func (Encryption) GetCertFingerprint

func (enc Encryption) GetCertFingerprint() CertFingerprintFunc

CertFingerprint computes the fingerprint of a certificate

func (Encryption) GetEncryptBasic

func (enc Encryption) GetEncryptBasic() EncryptBasicFunc

EncryptBasic implements basic encryption given the certificate (side effect because of random passphrase)

func (Encryption) GetPrivKey

func (enc Encryption) GetPrivKey() Key

PrivKey computes a new private key

func (Encryption) GetPrivKeyFingerprint

func (enc Encryption) GetPrivKeyFingerprint() PrivKeyFingerprintFunc

PrivKeyFingerprint computes the fingerprint of a private key

func (Encryption) GetPubKey

func (enc Encryption) GetPubKey() PubKeyFunc

PubKey computes a public key from a private key

func (Encryption) GetSignDigest

func (enc Encryption) GetSignDigest() SignDigestFunc

SignDigest computes the sha256 signature using a private key (side effect because of RSA blinding)

type Executor

type Executor = func([]byte) IOE.IOEither[error, EX.CommandOutput]

Executor is the signature of a function that executes a command with some input

func OpenSSL

func OpenSSL(args ...string) Executor

OpenSSL invokes the openSSL command using a fixed set of parameters

type Key

type Key = IOE.IOEither[error, []byte]

type PrivKeyFingerprintFunc

type PrivKeyFingerprintFunc = func([]byte) E.Either[error, []byte]

type PubKeyFunc

type PubKeyFunc = func([]byte) E.Either[error, []byte]

type SignDigestFunc

type SignDigestFunc = func([]byte) func([]byte) IOE.IOEither[error, []byte]

Jump to

Keyboard shortcuts

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