-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrypter.go
More file actions
48 lines (39 loc) · 1 KB
/
crypter.go
File metadata and controls
48 lines (39 loc) · 1 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
package sqlcrypter
import (
"io"
"sync"
)
var (
// crypter is the Crypterer used to encrypt and decrypt.
// This can only be set once by calling Init().
crypter Crypterer
// once ensures that Init() cannot be called more than once.
once sync.Once
)
type Crypterer interface {
Encrypt(w io.Writer, r io.Reader) error
Decrypt(w io.Writer, r io.Reader) error
}
// Init sets the encryption provider used by Encrypt() and Decrypt()
// and can only ever be called once. Repeated calls have no effect.
func Init(c Crypterer) {
once.Do(func() {
crypter = c
})
}
// Encrypt reads plaintext from an io.Reader
// and writes ciphertext to an io.Writer.
func Encrypt(w io.Writer, r io.Reader) error {
if crypter == nil {
return ErrCrypterNotInitialized
}
return crypter.Encrypt(w, r)
}
// Decrypt reads ciphertext from an io.Reader
// and writes plaintext to an io.Writer.
func Decrypt(w io.Writer, r io.Reader) error {
if crypter == nil {
return ErrCrypterNotInitialized
}
return crypter.Decrypt(w, r)
}