codec

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder[T any] func(value []byte) (T, error)

Decoder is an adapter allowing to use a function as a decoder.

var ByteDecoder Decoder[[]byte] = func(b []byte) ([]byte, error) {
	return b, nil
}

ByteDecoder represents a byte decoder.

var Int64Decoder Decoder[int64] = func(b []byte) (int64, error) {
	return strconv.ParseInt(string(b), 10, 64)
}

Int64Decoder represents an int64 decoder.

var NilDecoder Decoder[any] = func([]byte) (any, error) { return nil, nil }

NilDecoder is a decoder that always returns a nil, no matter the input.

var StringDecoder Decoder[string] = func(b []byte) (string, error) {
	return string(b), nil
}

StringDecoder represents a string decoder.

func ByteEncryptedDecoder

func ByteEncryptedDecoder() Decoder[[]byte]

ByteEncryptedDecoder represents a byte encrypted decoder.

func (Decoder[T]) Decode

func (f Decoder[T]) Decode(value []byte) (T, error)

Decode transforms byte data to the desired type.

type Decrypter

type Decrypter[T any] func([]byte, *rsa.PrivateKey) (T, error)

Decrypter is a function that decrypts a value.

var ByteDecrypter Decrypter[[]byte] = func(b []byte, key *rsa.PrivateKey) ([]byte, error) {
	hash := sha256.New()
	plainText, err := rsa.DecryptOAEP(hash, rand.Reader, key, b, nil)
	if err != nil {
		return nil, err
	}

	return plainText, nil
}

ByteDecrypter represents a byte decrypter.

var StringDecrypter Decrypter[string] = func(value []byte, key *rsa.PrivateKey) (string, error) {
	hash := sha256.New()
	plainText, err := rsa.DecryptOAEP(hash, rand.Reader, key, value, nil)
	if err != nil {
		return "", err
	}

	return string(plainText), nil
}

StringDecrypter represents a string decrypter.

func (Decrypter[T]) Decrypt

func (f Decrypter[T]) Decrypt(value []byte, key *rsa.PrivateKey) (T, error)

Decrypt is a function that decryptes a value.

type Encoder

type Encoder[T any] func(T) ([]byte, error)

Encoder is an adapter allowing to use a function as an encoder.

var ByteEncoder Encoder[[]byte] = func(v []byte) ([]byte, error) {
	return v, nil
}

ByteEncoder represents a byte encoder.

var EncryptedEncoder Encoder[string] = func(v string) ([]byte, error) {
	return []byte(v), nil
}

EncryptedEncoder represents an encrypted encoder.

var Int64Encoder Encoder[int64] = func(v int64) ([]byte, error) {
	return []byte(strconv.FormatInt(v, 10)), nil
}

Int64Encoder represents an int64 encoder.

var StringEncoder Encoder[string] = func(v string) ([]byte, error) {
	return []byte(v), nil
}

StringEncoder is a string encoder.

func (Encoder[T]) Encode

func (f Encoder[T]) Encode(value T) ([]byte, error)

Encode transforms the typed data to bytes.

type Encrypter

type Encrypter[T any] func(T, *rsa.PublicKey) ([]byte, error)

Encrypter is a function that encrypts a value.

var ByteEncrypter Encrypter[[]byte] = func(b []byte, key *rsa.PublicKey) ([]byte, error) {
	hash := sha256.New()
	ciperText, err := rsa.EncryptOAEP(hash, rand.Reader, key, b, nil)
	if err != nil {
		return nil, err
	}

	return ciperText, nil
}

ByteEncrypter represents a byte encrypter.

var StringEncrypter Encrypter[string] = func(b string, key *rsa.PublicKey) ([]byte, error) {
	hash := sha256.New()
	ciperText, err := rsa.EncryptOAEP(hash, rand.Reader, key, []byte(b), nil)
	if err != nil {
		return nil, err
	}

	return ciperText, nil
}

StringEncrypter represents a string encrypter.

func (Encrypter[T]) Encrypt

func (f Encrypter[T]) Encrypt(value T, key *rsa.PublicKey) ([]byte, error)

Encrypt is a function that encryptes a value.

Jump to

Keyboard shortcuts

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