-
Notifications
You must be signed in to change notification settings - Fork 72
/
decryptor.go
50 lines (39 loc) · 992 Bytes
/
decryptor.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 crypto
import (
"github.com/dobyte/due/crypto/ecc"
"github.com/dobyte/due/crypto/rsa"
"github.com/dobyte/due/log"
)
type Decryptor interface {
// Name 名称
Name() string
// Decrypt 解密
Decrypt(data []byte) ([]byte, error)
}
var decryptors = make(map[string]Decryptor)
func init() {
RegisterDecryptor(ecc.DefaultDecryptor)
RegisterDecryptor(rsa.DefaultDecryptor)
}
// RegisterDecryptor 注册解密器
func RegisterDecryptor(decryptor Decryptor) {
if decryptor == nil {
log.Fatal("can't register a invalid decryptor")
}
name := decryptor.Name()
if name == "" {
log.Fatal("can't register a decryptor without name")
}
if _, ok := decryptors[name]; ok {
log.Warnf("the old %s decryptor will be overwritten", name)
}
decryptors[name] = decryptor
}
// InvokeDecryptor 调用解密器
func InvokeDecryptor(name string) Decryptor {
decryptor, ok := decryptors[name]
if !ok {
log.Fatalf("%s decryptor is not registered", name)
}
return decryptor
}