-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrypter.go
More file actions
57 lines (46 loc) · 1.19 KB
/
Copy pathcrypter.go
File metadata and controls
57 lines (46 loc) · 1.19 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
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.
//
// An errors is returned if the Crypter is nil, so that encryption does
// not get silently disabled for the lifetime of the process.
func Init(c Crypterer) error {
if c == nil {
return ErrInitWithNil
}
once.Do(func() {
crypter = c
})
return nil
}
// 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)
}