-
Notifications
You must be signed in to change notification settings - Fork 171
/
encryptor.go
44 lines (37 loc) · 1.06 KB
/
encryptor.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
package miniProgram
import (
"encoding/base64"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/support"
)
type Encryptor struct {
BaseEncryptor *kernel.Encryptor
aes *support.AES
}
func NewEncryptor(appID, token, aesKey string) (*Encryptor, error) {
baseEncryptor, err := kernel.NewEncryptor(appID, token, aesKey)
if err != nil {
return nil, err
}
return &Encryptor{
BaseEncryptor: baseEncryptor,
aes: &support.AES{},
}, nil
}
func (encryptor Encryptor) DecryptData(encrypted string, sessionKey string, iv string) ([]byte, *support.CryptError) {
encryptData := encrypted
_key, errDecode := base64.StdEncoding.DecodeString(sessionKey)
if errDecode != nil {
return nil, support.NewCryptError(-1, errDecode.Error())
}
_iv, errDecode := base64.StdEncoding.DecodeString(iv)
if errDecode != nil {
return nil, support.NewCryptError(-1, errDecode.Error())
}
aes := support.AES{}
data, err := aes.Decrypt(encryptData, _key, _iv)
if err != nil {
return nil, err
}
return data, err
}