encryptedstring

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: MIT Imports: 11 Imported by: 0

README

encryptedstring

Small Go type implementation for automatically En/Decrypting a string when stored in a database. Also does JSON.

My interpretation of the article found here: https://www.vividcortex.com/blog/how-we-encrypt-data-in-mysql-with-go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrShortCipher      = errors.New("ciphertext too short.")
	ErrVersionMismatch  = errors.New("version mismatch.")
	ErrSrcNotBytes      = errors.New("EncryptedString Scan src not a []byte.")
	ErrNoSuchKey        = errors.New("No such key exists.")
	ErrKeyAlreadyExists = errors.New("Key with this name already exists.")
)

Functions

func AddKey

func AddKey(name string, key []byte) error

Types

type BlindIndexHash

type BlindIndexHash string

BlindIndexHash just does a sha512 HMAC hash. Good enough for queryable values that don't need to be crazy secure (most values), however these are NOT reversable. They are usually accompanied by an encrypted version though, so this is just to allow 1:1 queries. Note that the Valuer interface will hash the string autmatically if it is used as a bound parameter.

func (BlindIndexHash) GetBase64

func (b BlindIndexHash) GetBase64() (string, error)

GetBase64 returns the Base64 string value of the hashed value. Used for programmatically comparing values.

func (BlindIndexHash) GetHash

func (b BlindIndexHash) GetHash() ([]byte, error)

func (BlindIndexHash) Value

func (b BlindIndexHash) Value() (driver.Value, error)

type BlindIndexStrong

type BlindIndexStrong string

BlindIndexStrong should only be used for extremely sensitive pieces of data that need to be 1:1 queried, such as an SSN. We usually don't store these types of values, so please use BlindIndexHash instead.

type EncryptedString

type EncryptedString string

EncryptedString type is a string that will encrypt and decrypt on the fly when used with a database/sql/driver. It can also be encoding/json Marshaled and Unmarshaled with the same effect.

func (EncryptedString) Decrypt

func (e EncryptedString) Decrypt(ciphertext []byte) (string, error)

Decrypt is used by the Scanner to decrypt the value incoming from the DB

func (EncryptedString) Encrypt

func (e EncryptedString) Encrypt() ([]byte, error)

Encrypt encrypts the string using AES with GCM

func (EncryptedString) MarshalJSON

func (e EncryptedString) MarshalJSON() ([]byte, error)

func (*EncryptedString) Scan

func (e *EncryptedString) Scan(src interface{}) error

Scan calls decrypt on the string before

func (*EncryptedString) UnmarshalJSON

func (e *EncryptedString) UnmarshalJSON(b64 []byte) error

func (EncryptedString) Value

func (e EncryptedString) Value() (driver.Value, error)

Value calls encrypt on the string before storing it

Jump to

Keyboard shortcuts

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