security

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package security contains functions to encrypt and decrypt data. These functions where created/based upon: https://github.com/gtank/cryptopasta.

Index

Constants

View Source
const (
	// HashLength is the length of the hash byte slice.
	HashLength = 16
	// KeyLength is the length of the key byte slice.
	KeyLength = 32
)

Variables

View Source
var (
	// ErrMalformedData is returned with the provided data is erroneous.
	ErrMalformedData = errors.New("malformed data")
	// ErrInvalidKey is returned when the provided key is invalid.
	ErrInvalidKey = errors.New("invalid key")
	// ErrInvalidKeyLength is returned when the provided key is not the correct size.
	ErrInvalidKeyLength = errors.New("invalid key length, must be " + strconv.Itoa(KeyLength) + " bytes")
)

Functions

func ComparePasswordAndKey

func ComparePasswordAndKey(password []byte, key Key) bool

ComparePasswordAndKey compares the provided password with the provided key.

func Decrypt

func Decrypt(b []byte, key []byte) ([]byte, error)

Decrypt data encrypted with 256-bit AES-GCM encryption using the given key.

func Encrypt

func Encrypt(b []byte, key []byte) ([]byte, error)

Encrypt data with 256-bit AES-GCM encryption using the given key.

func NewSHA256FromPassword added in v0.3.0

func NewSHA256FromPassword(password []byte) ([]byte, error)

NewSHA256FromPassword creates a new SHA256 from the provided password.

Types

type Key

type Key struct {
	Value, Salt []byte
}

Key contains a hashed value and the salt used to hash it.

func NewKey

func NewKey() (Key, error)

NewHash generates a new random key with argon2id.

func NewKeyFromPassword

func NewKeyFromPassword(password []byte) (Key, error)

NewKeyFromPassword creates a new key from the provided password using argon2id.

func (*Key) Decode

func (k *Key) Decode(b []byte) error

Decodes the provided bytes into a Key. Format: base64(salt)$base64(hash)

func (Key) Encode

func (k Key) Encode() []byte

Encode the key for persistance. Format: base64(salt)$base64(hash)

func (Key) Valid

func (k Key) Valid() bool

Valid checks if a key is valid

Jump to

Keyboard shortcuts

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