ioeither

package
v1.0.39 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Overview

Copyright 2023 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var (

	// OpenSSLDecryption returns the decryption environment using OpenSSL
	OpenSSLDecryption = IO.MakeIO(func() Decryption {
		return Decryption{
			DecryptBasic: OpenSSLDecryptBasic,
		}
	})

	// CryptoDecryption returns the decryption environment using golang crypto
	CryptoDecryption = IO.MakeIO(func() Decryption {
		return Decryption{
			DecryptBasic: CryptoDecryptBasic,
		}
	})

	// DefaultDecryption detects the decryption environment
	DefaultDecryption = F.Pipe1(
		validOpenSSL,
		IOE.Fold(F.Constant1[error](CryptoDecryption), F.Constant1[string](OpenSSLDecryption)),
	)
)
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)

	// CryptoAsymmetricDecrypt decrypts a piece of text using a private key
	CryptoAsymmetricDecrypt = cryptoAsymmetricDecrypt(privToRsaKey)
)
View Source
var (

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

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

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

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

	OpenSSLAsymmetricDecrypt = handle(asymmetricDecrypt)

	OpenSSLSymmetricEncrypt = 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 CryptoDecryptBasic added in v1.0.1

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

CryptoDecryptBasic implements basic decryption using golang crypto libraries given the private key

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 CryptoSymmetricDecrypt added in v1.0.1

func CryptoSymmetricDecrypt(srcText string) func([]byte) IOE.IOEither[error, []byte]

CryptoSymmetricDecrypt encrypts a set of bytes using a password

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 OpenSSLSymmetricDecrypt added in v1.0.1

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

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)

Types

type CertFingerprintFunc

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

type Decryption added in v1.0.1

type Decryption struct {
	// DecryptBasic implements basic decryption given the private key
	DecryptBasic func(privKey []byte) func(string) IOE.IOEither[error, []byte]
}

Decryption captures the crypto functions required to implement the source providers

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