Documentation
¶
Index ¶
- Variables
- func Decrypt(w io.Writer, r io.Reader) error
- func Encrypt(w io.Writer, r io.Reader) error
- func GenerateBytes(n int) ([]byte, error)
- func Init(c Crypterer) error
- type Crypterer
- type EncryptedBytes
- func (e EncryptedBytes) Bytes() []byte
- func (e *EncryptedBytes) GormDBDataType(db *gorm.DB, field *schema.Field) string
- func (e *EncryptedBytes) GormDataType() string
- func (e EncryptedBytes) MarshalJSON() ([]byte, error)
- func (e EncryptedBytes) Plaintext() string
- func (e *EncryptedBytes) Scan(value any) error
- func (e EncryptedBytes) String() string
- func (e *EncryptedBytes) UnmarshalJSON(data []byte) error
- func (e EncryptedBytes) Value() (driver.Value, error)
- type NullEncryptedBytes
- func (n NullEncryptedBytes) Bytes() []byte
- func (n NullEncryptedBytes) BytesValue() ([]byte, error)
- func (n *NullEncryptedBytes) GormDBDataType(db *gorm.DB, field *schema.Field) string
- func (n *NullEncryptedBytes) GormDataType() string
- func (n NullEncryptedBytes) MarshalJSON() ([]byte, error)
- func (n NullEncryptedBytes) Plaintext() string
- func (n *NullEncryptedBytes) Scan(value any) error
- func (n *NullEncryptedBytes) ScanBytes(src []byte) error
- func (n NullEncryptedBytes) String() string
- func (n *NullEncryptedBytes) UnmarshalJSON(data []byte) error
- func (n NullEncryptedBytes) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInitWithNil indicates that Init() has been called with a nil Crypterer. ErrInitWithNil = errors.New("sqlcrypter: Init() called with nil crypter") // ErrCrypterNotInitialized indicates that Init() has not been called // with a valid Crypterer before Encrypt()/Decrypt() usage. ErrCrypterNotInitialized = errors.New("sqlcrypter: crypter is not initialized") )
Functions ¶
func GenerateBytes ¶
GenerateBytes generates random bytes of n length.
Types ¶
type EncryptedBytes ¶
type EncryptedBytes []byte
func NewEncryptedBytes ¶
func NewEncryptedBytes(s string) EncryptedBytes
func (EncryptedBytes) Bytes ¶
func (e EncryptedBytes) Bytes() []byte
func (*EncryptedBytes) GormDBDataType ¶
func (*EncryptedBytes) GormDataType ¶
func (e *EncryptedBytes) GormDataType() string
func (EncryptedBytes) MarshalJSON ¶
func (e EncryptedBytes) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler interface
func (EncryptedBytes) Plaintext ¶ added in v0.3.0
func (e EncryptedBytes) Plaintext() string
Plaintext returns the decrypted contents as a string.
It is the caller's responsibility to ensure this value is not logged or otherwise exposed.
func (*EncryptedBytes) Scan ¶
func (e *EncryptedBytes) Scan(value any) error
Scan implements the scanner interface
func (EncryptedBytes) String ¶
func (e EncryptedBytes) String() string
String intentionally returns a redacted placeholder to safeguard the plaintext from being inadventerly leaked in logs, stack traces, etc.
To deliberately access the plaintext, call Plaintext() or Bytes().
func (*EncryptedBytes) UnmarshalJSON ¶
func (e *EncryptedBytes) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler interface
type NullEncryptedBytes ¶ added in v0.3.0
type NullEncryptedBytes struct {
Data EncryptedBytes
Valid bool
}
NullEncryptedBytes is a nullable encrypted column value for database/sql, GORM, JSON, and pgx/sqlc (BYTEA) workflows.
Semantics:
- Valid == false: SQL NULL; JSON null; Plaintext/Bytes are empty/nil.
- Valid == true: plaintext is stored encrypted, including empty plaintext (distinct from SQL NULL).
func NewNullEncryptedBytes ¶ added in v0.3.0
func NewNullEncryptedBytes(s string) NullEncryptedBytes
NewNullEncryptedBytes returns a present value with plaintext s (empty string allowed).
func NullEncryptedBytesNull ¶ added in v0.3.0
func NullEncryptedBytesNull() NullEncryptedBytes
NullEncryptedBytesNull returns an absent (SQL NULL) value.
func (NullEncryptedBytes) Bytes ¶ added in v0.3.0
func (n NullEncryptedBytes) Bytes() []byte
Bytes returns a slice view of plaintext when Valid; otherwise nil. The returned slice aliases Data; mutating it mutates the stored plaintext.
func (NullEncryptedBytes) BytesValue ¶ added in v0.3.0
func (n NullEncryptedBytes) BytesValue() ([]byte, error)
BytesValue implements pgtype.BytesValuer for pgx/sqlc BYTEA columns.
func (*NullEncryptedBytes) GormDBDataType ¶ added in v0.3.0
func (*NullEncryptedBytes) GormDataType ¶ added in v0.3.0
func (n *NullEncryptedBytes) GormDataType() string
func (NullEncryptedBytes) MarshalJSON ¶ added in v0.3.0
func (n NullEncryptedBytes) MarshalJSON() ([]byte, error)
MarshalJSON encodes absent values as JSON null; present values as base64 ciphertext.
func (NullEncryptedBytes) Plaintext ¶ added in v0.3.0
func (n NullEncryptedBytes) Plaintext() string
Plaintext returns decrypted plaintext when Valid; otherwise "".
func (*NullEncryptedBytes) Scan ¶ added in v0.3.0
func (n *NullEncryptedBytes) Scan(value any) error
Scan implements sql.Scanner.
func (*NullEncryptedBytes) ScanBytes ¶ added in v0.3.0
func (n *NullEncryptedBytes) ScanBytes(src []byte) error
ScanBytes implements pgtype.BytesScanner for pgx/sqlc BYTEA columns.
func (NullEncryptedBytes) String ¶ added in v0.3.0
func (n NullEncryptedBytes) String() string
String intentionally returns a redacted placeholder.
func (*NullEncryptedBytes) UnmarshalJSON ¶ added in v0.3.0
func (n *NullEncryptedBytes) UnmarshalJSON(data []byte) error
UnmarshalJSON decodes JSON null as absent; otherwise expects base64 ciphertext bytes.