-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgocrypt.go
76 lines (68 loc) · 1.75 KB
/
gocrypt.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
package gocrypt
// GocryptInterface is facing the format gocrypt option library
type GocryptInterface interface {
Encrypt(interface{}) error
Decrypt(interface{}) error
}
// GocryptOption is facing the format encryption and decryption format
type GocryptOption interface {
Encrypt(plainText []byte) (string, error)
Decrypt(chipherText []byte) (string, error)
}
//
// Option contains an option from initial algorithm encryptioin & decryption.
//
type Option struct {
AESOpt GocryptOption
DESOpt GocryptOption
RC4Opt GocryptOption
Custom map[string]GocryptOption
Prefix string
Postfix string
}
//
// New create and initialize new option for struct field encryption.
//
// It needs option from aes, rc4, or des for initialitaion
//
func New(opt *Option) *Option {
return opt
}
//
// Encrypt is function to set struct field encrypted
//
func (opt *Option) Encrypt(structVal interface{}) error {
return read(structVal, opt.encrypt)
}
//
// Decrypt is function to set struct field decrypted
//
func (opt *Option) Decrypt(structVal interface{}) error {
return read(structVal, opt.decrypt)
}
func (opt *Option) encrypt(algo string, plainText string) (string, error) {
plainByte := []byte(plainText)
switch algo {
case "aes":
return opt.AESOpt.Encrypt(plainByte)
case "des":
return opt.DESOpt.Encrypt(plainByte)
case "rc4":
return opt.RC4Opt.Encrypt(plainByte)
default:
return opt.AESOpt.Encrypt(plainByte)
}
}
func (opt *Option) decrypt(algo string, chipperText string) (string, error) {
chipperByte := []byte(chipperText)
switch algo {
case "aes":
return opt.AESOpt.Decrypt(chipperByte)
case "des":
return opt.DESOpt.Decrypt(chipperByte)
case "rc4":
return opt.RC4Opt.Decrypt(chipperByte)
default:
return opt.AESOpt.Decrypt(chipperByte)
}
}