-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnull_encrypted_bytes.go
More file actions
180 lines (163 loc) · 4.28 KB
/
Copy pathnull_encrypted_bytes.go
File metadata and controls
180 lines (163 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package sqlcrypter
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/jackc/pgx/v5/pgtype"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// 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).
type NullEncryptedBytes struct {
Data EncryptedBytes
Valid bool
}
// NewNullEncryptedBytes returns a present value with plaintext s (empty string allowed).
func NewNullEncryptedBytes(s string) NullEncryptedBytes {
var data EncryptedBytes
if s == "" {
data = EncryptedBytes([]byte{})
} else {
data = EncryptedBytes([]byte(s))
}
return NullEncryptedBytes{Data: data, Valid: true}
}
// NullEncryptedBytesNull returns an absent (SQL NULL) value.
func NullEncryptedBytesNull() NullEncryptedBytes {
return NullEncryptedBytes{Valid: false}
}
func (n *NullEncryptedBytes) GormDataType() string {
return "nullencryptedbytes"
}
func (n *NullEncryptedBytes) GormDBDataType(db *gorm.DB, field *schema.Field) string {
switch db.Name() {
case "mysql":
return "binary"
case "postgres":
return "bytea"
case "sqlite":
return "blob"
case "sqlserver":
return "varbinary"
default:
return ""
}
}
// String intentionally returns a redacted placeholder.
func (n NullEncryptedBytes) String() string {
return "[REDACTED]"
}
// Plaintext returns decrypted plaintext when Valid; otherwise "".
func (n NullEncryptedBytes) Plaintext() string {
if !n.Valid {
return ""
}
return string(n.Data)
}
// Bytes returns a slice view of plaintext when Valid; otherwise nil.
// The returned slice aliases Data; mutating it mutates the stored plaintext.
func (n NullEncryptedBytes) Bytes() []byte {
if !n.Valid {
return nil
}
return n.Data[:]
}
// Scan implements sql.Scanner.
func (n *NullEncryptedBytes) Scan(value any) error {
if value == nil {
n.Valid = false
n.Data = nil
return nil
}
b, ok := value.([]byte)
if !ok {
return fmt.Errorf("sqlcrypter: NullEncryptedBytes.Scan expected []byte, got %T", value)
}
if b == nil {
n.Valid = false
n.Data = nil
return nil
}
pt, err := decryptCiphertextBytes(b)
if err != nil {
return err
}
n.Data = pt
n.Valid = true
return nil
}
// Value implements driver.Valuer.
func (n NullEncryptedBytes) Value() (driver.Value, error) {
if !n.Valid {
//nolint:nilnil // driver.Valuer: nil value with nil error means SQL NULL
return nil, nil
}
return encryptPlaintextBytes(n.Data)
}
// MarshalJSON encodes absent values as JSON null; present values as base64 ciphertext.
func (n NullEncryptedBytes) MarshalJSON() ([]byte, error) {
if !n.Valid {
return []byte("null"), nil
}
ciphertext, err := encryptPlaintextBytes(n.Data)
if err != nil {
return nil, err
}
return json.Marshal(ciphertext)
}
// UnmarshalJSON decodes JSON null as absent; otherwise expects base64 ciphertext bytes.
func (n *NullEncryptedBytes) UnmarshalJSON(data []byte) error {
var ciphertext []byte
if err := json.Unmarshal(data, &ciphertext); err != nil {
return err
}
if ciphertext == nil {
n.Valid = false
n.Data = nil
return nil
}
n.Valid = true
return n.Scan(ciphertext)
}
// ScanBytes implements pgtype.BytesScanner for pgx/sqlc BYTEA columns.
func (n *NullEncryptedBytes) ScanBytes(src []byte) error {
if src == nil {
n.Valid = false
n.Data = nil
return nil
}
// src is only valid until the next database call; copy before Scan.
b := append([]byte(nil), src...)
return n.Scan(b)
}
// BytesValue implements pgtype.BytesValuer for pgx/sqlc BYTEA columns.
func (n NullEncryptedBytes) BytesValue() ([]byte, error) {
v, err := n.Value()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
b, ok := v.([]byte)
if !ok {
return nil, fmt.Errorf("sqlcrypter: NullEncryptedBytes.BytesValue expected []byte, got %T", v)
}
return b, nil
}
var (
_ driver.Valuer = &NullEncryptedBytes{}
_ sql.Scanner = &NullEncryptedBytes{}
_ json.Marshaler = &NullEncryptedBytes{}
_ json.Unmarshaler = &NullEncryptedBytes{}
_ fmt.Stringer = &NullEncryptedBytes{}
_ pgtype.BytesScanner = &NullEncryptedBytes{}
_ pgtype.BytesValuer = &NullEncryptedBytes{}
)