-
Notifications
You must be signed in to change notification settings - Fork 52
/
signer_options.go
50 lines (40 loc) · 1.33 KB
/
signer_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
46
47
48
49
50
package ecc
import (
"github.com/dobyte/due/config"
"github.com/dobyte/due/crypto/hash"
"strings"
)
const (
defaultSignerHashKey = "config.crypto.rsa.signer.hash"
defaultSignerDelimiterKey = "config.crypto.rsa.signer.delimiter"
defaultSignerPrivateKeyKey = "config.crypto.rsa.signer.privateKey"
)
type SignerOption func(o *signerOptions)
type signerOptions struct {
// hash算法。支持sha1、sha224、sha256、sha384、sha512
// 默认为sha256
hash hash.Hash
// 签名分隔符。
delimiter string
// 私钥。可设置文件路径或私钥串
privateKey string
}
func defaultSignerOptions() *signerOptions {
return &signerOptions{
hash: hash.Hash(strings.ToLower(config.Get(defaultSignerHashKey).String())),
delimiter: config.Get(defaultSignerDelimiterKey, " ").String(),
privateKey: config.Get(defaultSignerPrivateKeyKey).String(),
}
}
// WithSignerHash 设置加密hash算法
func WithSignerHash(hash hash.Hash) SignerOption {
return func(o *signerOptions) { o.hash = hash }
}
// WithSignerDelimiter 设置签名分割符
func WithSignerDelimiter(delimiter string) SignerOption {
return func(o *signerOptions) { o.delimiter = delimiter }
}
// WithSignerPrivateKey 设置解密私钥
func WithSignerPrivateKey(privateKey string) SignerOption {
return func(o *signerOptions) { o.privateKey = privateKey }
}