/
viper.go
70 lines (58 loc) · 1.15 KB
/
viper.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
package cipher
import (
"errors"
"github.com/spf13/viper"
"strings"
)
type LocalCerts struct {
Path string
HashName string
}
func LoadPublic(v *viper.Viper) (PublicKeyCipher, error) {
config := new(LocalCerts)
cipherViper := v.Sub("cipher")
if cipherViper == nil {
return nil, errors.New("no cipher to load")
}
err := cipherViper.Unmarshal(config)
if err != nil {
return nil, err
}
// check for noop
if strings.ToLower(config.HashName) == "noop" {
return new(NOOP), nil
}
loadConfg := LoadConfig{
&BasicHashLoader{
config.HashName,
},
&FileLoader{
Path: config.Path,
},
}
return LoadPublicKey(loadConfg)
}
func LoadPrivate(v *viper.Viper) (PrivateKeyCipher, error) {
config := new(LocalCerts)
cipherViper := v.Sub("cipher")
if cipherViper == nil {
return nil, errors.New("no cipher to load")
}
err := cipherViper.Unmarshal(config)
if err != nil {
return nil, err
}
// check for noop
if strings.ToLower(config.HashName) == "noop" {
return new(NOOP), nil
}
loadConfg := LoadConfig{
&BasicHashLoader{
config.HashName,
},
&FileLoader{
Path: config.Path,
},
}
return LoadPrivateKey(loadConfg)
}