-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
83 lines (76 loc) · 2.76 KB
/
secret.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
package crypto
import (
"crypto/rand"
"github.com/minio/sio"
"io"
)
/*
NewSecretBox returns a SecretBox object with a provided key.
Here is the validation code written for Python:
import os,binascii
key=binascii.b2a_base64(os.urandom(32),newline=False)
print(key.decode('us-ascii'))
*/
func NewSecretBox(key []byte) SecretBox {
streamConfig := sio.Config{
Key: key,
Rand: rand.Reader,
}
var (
encryptReader = func(src io.Reader) (io.Reader, error) {
return sio.EncryptReader(src, streamConfig)
}
decryptReader = func(src io.Reader) (io.Reader, error) {
return sio.DecryptReader(src, streamConfig)
}
encryptWriter = func(dst io.Writer) (io.WriteCloser, error) {
return sio.EncryptWriter(dst, streamConfig)
}
decryptWriter = func(dst io.Writer) (io.WriteCloser, error) {
return sio.DecryptWriter(dst, streamConfig)
}
)
return &secretBoxImpl{
encryptReaderGen: encryptReader,
decryptReaderGen: decryptReader,
encryptWriterGen: encryptWriter,
decryptWriterGen: decryptWriter,
}
}
// SecretBox is an encryption and decryption provider.
type SecretBox interface {
// NewEncryptReader returns EncryptReader from given io.Reader.
NewEncryptReader(io.Reader) (io.Reader, error)
// NewDecryptReader returns DecryptReader from given io.Reader.
NewDecryptReader(src io.Reader) (io.Reader, error)
// NewEncryptWriter returns EncryptWriter from given io.Writer.
NewEncryptWriter(dst io.Writer) (io.WriteCloser, error)
// NewDecryptWriter returns DecryptWriter from given io.Writer.
NewDecryptWriter(dst io.Writer) (io.WriteCloser, error)
// EncryptedSize returns encrypted data size from given raw data size.
EncryptedSize(size uint64) (uint64, error)
// DecryptedSize returns decrypted data size from given raw data size.
DecryptedSize(size uint64) (uint64, error)
}
type secretBoxImpl struct {
encryptReaderGen func(src io.Reader) (io.Reader, error)
decryptReaderGen func(src io.Reader) (io.Reader, error)
encryptWriterGen func(dst io.Writer) (io.WriteCloser, error)
decryptWriterGen func(dst io.Writer) (io.WriteCloser, error)
}
// 인터페이스가 실제 dto랑 호환되는가
var _ SecretBox = (*secretBoxImpl)(nil)
func (x *secretBoxImpl) NewEncryptReader(r io.Reader) (io.Reader, error) {
return x.encryptReaderGen(r)
}
func (x *secretBoxImpl) NewDecryptReader(r io.Reader) (io.Reader, error) {
return x.decryptReaderGen(r)
}
func (x *secretBoxImpl) NewEncryptWriter(w io.Writer) (io.WriteCloser, error) {
return x.encryptWriterGen(w)
}
func (x *secretBoxImpl) NewDecryptWriter(w io.Writer) (io.WriteCloser, error) {
return x.decryptWriterGen(w)
}
func (x *secretBoxImpl) EncryptedSize(size uint64) (uint64, error) { return sio.EncryptedSize(size) }
func (x *secretBoxImpl) DecryptedSize(size uint64) (uint64, error) { return sio.DecryptedSize(size) }