-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
189 lines (153 loc) · 4.28 KB
/
value.go
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
181
182
183
184
185
186
187
188
189
package silent
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"unicode/utf8"
)
// EncryptedValueFactory is a generic type factory for creating custom [EncryptedValue] types.
// To define a new EncryptedValue type, create a unique dummy type and use it as the generic parameter:
//
// type dummy1 struct{} // this won't be used in your code
// type MyEncryptedValue = EncryptedValueFactory[dummy1]
type EncryptedValueFactory[T any] []byte
type dummy struct{}
// EncryptedValue is a built-in type that is automatically encrypted when written to, and decrypted when read from, the database.
type EncryptedValue = EncryptedValueFactory[dummy]
// Crypter is an interface that can be implemented to provide a custom encryption strategy
type Crypter interface {
Encrypt(data []byte) ([]byte, error)
Decrypt(data []byte) ([]byte, error)
}
type crypterMapping struct {
Zero any
Crypter Crypter
}
var crypters []crypterMapping
// BindCrypterTo binds a crypter instance to a specific EncryptedValue type.
// Example usage:
//
// BindCrypterTo[silent.EncryptedValue](&crypter)
func BindCrypterTo[F EncryptedValueFactory[T], T any](c Crypter) {
// this full scan loop is about 10x faster than map in this scenario
for _, c := range crypters {
if _, ok := c.Zero.(T); ok {
panic("misconfigurtion: crypter already registered")
}
}
var zero T
crypters = append(crypters, crypterMapping{
Zero: zero,
Crypter: c,
})
}
func getCrypterFor[T any]() Crypter {
for _, c := range crypters {
if _, ok := c.Zero.(T); ok {
return c.Crypter
}
}
panic("misconfiguration: no crypter registered for this type")
}
// String returns a string representation of the EncryptedValue
func (v EncryptedValueFactory[T]) String() string {
return fmt.Sprintf("EncryptedValue(%s)", string(v))
}
// MarshalJSON encrypts the value and marshals it into JSON format.
// - If the value is empty, it is marshalled as a JSON representation of an empty string ("").
// - If the encrypted data forms a valid UTF-8 string, it is marshaled as a string prefixed with '#'.
// - Otherwise, the data is marshaled as a base64-encoded string.
func (v EncryptedValueFactory[T]) MarshalJSON() ([]byte, error) {
if len(v) == 0 {
return []byte(`""`), nil
}
crypter := getCrypterFor[T]()
encData, err := crypter.Encrypt(v)
if err != nil {
return nil, err
}
if utf8.Valid(encData) {
var buf bytes.Buffer
buf.Grow(len(encData) + 3)
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
err := enc.Encode("#" + string(encData)) // will be encoded as string prepended by #
if err != nil {
return nil, err
}
res := buf.Bytes()
res = res[:len(res)-1] // trim trailing newline
return res, nil
} else {
return json.Marshal(encData) // will be encoded as base64
}
}
// UnmarshalJSON decrypts the value from JSON.
func (v *EncryptedValueFactory[T]) UnmarshalJSON(data []byte) error {
if s := string(data); s == `""` || s == `null` {
*v = nil
return nil
}
crypter := getCrypterFor[T]()
var encData []byte
// string or base64?
if len(data) >= 2 && data[1] == '#' {
var target string
err := json.Unmarshal(data, &target)
if err != nil {
return err
}
encData = []byte(target[1:])
} else {
err := json.Unmarshal(data, &encData)
if err != nil {
return err
}
}
var err error
*v, err = crypter.Decrypt(encData)
return err
}
// Value is a driver.Valuer implementation. It encrypts the value and returns a byte slice suitable for database storage.
func (v EncryptedValueFactory[T]) Value() (driver.Value, error) {
if len(v) == 0 {
return []byte{}, nil
}
crypter := getCrypterFor[T]()
encData, err := crypter.Encrypt(v)
return encData, err
}
// Scan is a sql.Scanner implementation. It decrypts the value from the database.
func (v *EncryptedValueFactory[T]) Scan(value interface{}) error {
crypter := getCrypterFor[T]()
switch t := value.(type) {
case nil:
*v = nil
return nil
case []byte:
if len(t) == 0 {
*v = nil
return nil
}
data, err := crypter.Decrypt(t)
if err != nil {
return err
}
*v = data
return nil
case string:
if t == "" {
*v = nil
return nil
}
data, err := crypter.Decrypt([]byte(t))
if err != nil {
return err
}
*v = data
return nil
default:
return fmt.Errorf("unable to scan %T into EncryptedValue", value)
}
}