encryption

package
v9.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Crypter

type Crypter struct {
	// contains filtered or unexported fields
}

Crypter contains the AEAD cipher to use for encryption and decryption.

func NewAESGCM

func NewAESGCM(key []byte, nonceGenerator NonceGenerator) (*Crypter, error)

NewAESGCM creates a new Cryptor initialised with an AES-GCM cipher from the supplied key. The key is sensitive, never share it openly.

The key should be 16 bytes (AES-128) or 32 bytes (AES-256) long.

If you want to convert a passphrase to a key, use a suitable package like bcrypt or scrypt.

TODO: add a nonceGenerator validator i.e. call it 1000 times, ensures no dupes.

func NewAESGCMWithRandomNonceGenerator

func NewAESGCMWithRandomNonceGenerator(key []byte) (*Crypter, error)

NewAESGCMWithRandomNonceGenerator creates a new Cryptor initialised with an AES-GCM cipher from the supplied key and the default nonce generator.

func NewChaCha20Poly1305

func NewChaCha20Poly1305(key []byte, nonceGenerator NonceGenerator) (*Crypter, error)

NewChaCha20Poly1305 creates a new Cryptor initialised with a ChaCha20Poly1305 cipher from the supplied key. The key is sensitive, never share it openly.

The key should be 32 bytes long.

If you want to convert a passphrase to a key, you can use a function such as Argon2.

TODO: add a nonceGenerator validator i.e. call it 1000 times, ensures no dupes.

func NewChaCha20Poly1305WithRandomNonceGenerator

func NewChaCha20Poly1305WithRandomNonceGenerator(key []byte) (*Crypter, error)

NewChaCha20Poly1305WithRandomNonceGenerator creates a new Cryptor initialised with a ChaCha20Poly1305 cipher from the supplied key and the default nonce generator.

func NewCrypter

func NewCrypter(aead cipher.AEAD, kind string, nonceGenerator NonceGenerator) *Crypter

NewCrypter creates a new initialised Crypter.

func (Crypter) Decrypt

func (c Crypter) Decrypt(ciphertext, nonce []byte) ([]byte, error)

Decrypt performs the decryption of the provided ciphertext with the key associated with this Crypter and the supplied nonce. This must be the same nonce that was used to encrypt the ciphertext. The nonce is not sensitive.

func (Crypter) Encrypt

func (c Crypter) Encrypt(plaintext []byte) (ciphertext, nonce []byte, err error)

Encrypt performs the encryption of the provided plaintext with the key associated with this Crypter and the supplied nonce. The nonce is generated from c.nonceGenerator.

func (Crypter) Kind

func (c Crypter) Kind() string

type NonceGenerator

type NonceGenerator interface {
	Generate() ([]byte, error)
}

NonceGenerator defines the behaviour of a Nonce Generator type.

type RandomNonceGenerator

type RandomNonceGenerator struct {
	// contains filtered or unexported fields
}

RandomNonceGenerator is a random generator of nonce of the specified size.

func NewRandomNonceGenerator

func NewRandomNonceGenerator(size int) *RandomNonceGenerator

NewRandomNonceGenerator creates a new initialised RandomNonceGenerator of specified size.

func (RandomNonceGenerator) Generate

func (ng RandomNonceGenerator) Generate() ([]byte, error)

For a 12-byte nonce, never use more than 2^32 random nonces with a given key because of the risk of a repeat and cipher .

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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