-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrypter_bytes.go
More file actions
30 lines (27 loc) · 924 Bytes
/
Copy pathcrypter_bytes.go
File metadata and controls
30 lines (27 loc) · 924 Bytes
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
package sqlcrypter
import "bytes"
// encryptPlaintextBytes encrypts plaintext and returns ciphertext bytes.
func encryptPlaintextBytes(plaintext []byte) ([]byte, error) {
reader := bytes.NewReader(plaintext)
writer := new(bytes.Buffer)
if err := Encrypt(writer, reader); err != nil {
return nil, err
}
ct := writer.Bytes()
// Avoid returning a nil []byte slice: database/sql and JSON callers treat
// nil []byte as NULL/absent; NullEncryptedBytes needs a non-nil ciphertext
// for encrypted-empty plaintext (distinct from SQL NULL).
if ct == nil {
ct = []byte{}
}
return ct, nil
}
// decryptCiphertextBytes decrypts ciphertext into plaintext bytes.
func decryptCiphertextBytes(ciphertext []byte) (EncryptedBytes, error) {
reader := bytes.NewReader(ciphertext)
writer := new(bytes.Buffer)
if err := Decrypt(writer, reader); err != nil {
return nil, err
}
return EncryptedBytes(writer.Bytes()), nil
}