-
Notifications
You must be signed in to change notification settings - Fork 72
/
encryptor_options.go
45 lines (36 loc) · 1.23 KB
/
encryptor_options.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
package ecc
import (
"github.com/dobyte/due/config"
"github.com/dobyte/due/utils/xconv"
)
const (
defaultEncryptorShareInfo1Key = "config.crypto.ecc.encryptor.s1"
defaultEncryptorShareInfo2Key = "config.crypto.ecc.encryptor.s2"
defaultEncryptorPublicKeyKey = "config.crypto.ecc.encryptor.publicKey"
)
type EncryptorOption func(o *encryptorOptions)
type encryptorOptions struct {
// 共享信息。加解密时必需一致
// 默认为空
s1 []byte
// 共享信息。加解密时必需一致
// 默认为空
s2 []byte
// 公钥。可设置文件路径或公钥串
publicKey string
}
func defaultEncryptorOptions() *encryptorOptions {
return &encryptorOptions{
s1: config.Get(defaultEncryptorShareInfo1Key).Bytes(),
s2: config.Get(defaultEncryptorShareInfo2Key).Bytes(),
publicKey: config.Get(defaultEncryptorPublicKeyKey).String(),
}
}
// WithEncryptorShareInfo 设置共享信息
func WithEncryptorShareInfo(s1, s2 string) EncryptorOption {
return func(o *encryptorOptions) { o.s1, o.s2 = xconv.StringToBytes(s1), xconv.StringToBytes(s2) }
}
// WithEncryptorPublicKey 设置加密公钥
func WithEncryptorPublicKey(publicKey string) EncryptorOption {
return func(o *encryptorOptions) { o.publicKey = publicKey }
}