encrypt

package
v2.20.2 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDecryptFailed = errors.New("decryption failed")
)

Functions

This section is empty.

Types

type SecureData

type SecureData struct {
	Prefix []byte
}

func NewSecureData

func NewSecureData(prefix string) *SecureData

NewSecureData is used to securely read and write encrypted data

func (*SecureData) Decrypt

func (s *SecureData) Decrypt(data []byte, passphrase string) ([]byte, error)

Decrypt decrypt the data. The data should be stored with a 32 byte salt which is append at the end of the data

func (*SecureData) DecryptFile

func (s *SecureData) DecryptFile(filename string, passphrase string) ([]byte, error)

func (*SecureData) DecryptHex

func (s *SecureData) DecryptHex(data string, passphrase string) (string, error)

func (*SecureData) DecryptString

func (s *SecureData) DecryptString(data string, passphrase string) (string, error)

func (*SecureData) DeriveKey

func (s *SecureData) DeriveKey(password, salt []byte) ([]byte, []byte, error)

DeriveKey creates a secure key from a given password. It also accepts a salt which is used to increase the security of the key to prevent Rainbow tables attacks. If the salt is nil, then a secure salt will be generated using scrypt.

func (*SecureData) Encrypt

func (s *SecureData) Encrypt(data []byte, passphrase string) ([]byte, error)

Encrypt encrypts the data with a passphrase. Salt is used on the passphrase and stored in the last 32 bytes of the data (appended decrypted) Example: asdfadsfasdfasdfasdf<salt>

func (*SecureData) EncryptFile

func (s *SecureData) EncryptFile(filename string, data []byte, passphrase string)

func (*SecureData) EncryptString

func (s *SecureData) EncryptString(data string, passphrase string) (string, error)

EncryptString encryptes a string to a hex encoded string using a passphrase and salt. A prefix is also added to it (if defined)

func (*SecureData) FromHex

func (s *SecureData) FromHex(data []byte) ([]byte, error)

func (*SecureData) FromHexString

func (s *SecureData) FromHexString(data string) ([]byte, error)

func (*SecureData) IsEncrypted

func (s *SecureData) IsEncrypted(data string) int

IsEncrypted returns if the given data is encrypted or not. The data is encrypted if it starts with the encryption marker. If an empty string is used as the encryption prefix, then the it can not be reliably checked if the data is enrypted or not. 1 - data is encrypted 0 - data is not encrypted -1 - data encryption is unknown (could be encrypted or not)

func (*SecureData) IsEncryptedBytes

func (s *SecureData) IsEncryptedBytes(data []byte) int

IsEncryptedBytes returns if the given data is encrypted or not. The data is encrypted if it starts with the encryption marker. If an empty string is used as the encryption prefix, then the it can not be reliably checked if the data is enrypted or not. 1 - data is encrypted 0 - data is not encrypted -1 - data encryption is unknown (could be encrypted or not)

func (*SecureData) ToHexString

func (s *SecureData) ToHexString(data []byte) string

func (*SecureData) TryDecryptString

func (s *SecureData) TryDecryptString(data string, passphrase string) (string, error)

TryDecryptString tries to decrypt a string. If a encryption prefex string is used, then the data will only be decrypted if the input data has the prefix, otherwise it will be returned as is.

func (*SecureData) TryEncryptString

func (s *SecureData) TryEncryptString(data string, passphrase string) (string, error)

TryEncryptString encrypts a string only if it is not already encrypted

Jump to

Keyboard shortcuts

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