Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化小程序解密 #262

Merged
merged 1 commit into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions miniprogram/basic/basic.go

This file was deleted.

58 changes: 24 additions & 34 deletions miniprogram/basic/decrypt.go → miniprogram/encryptor/encryptor.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package basic
package encryptor

import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"

"github.com/silenceper/wechat/v2/miniprogram/context"
)

//Encryptor struct
type Encryptor struct {
*context.Context
}

//NewEncryptor 实例
func NewEncryptor(context *context.Context) *Encryptor {
basic := new(Encryptor)
basic.Context = context
return basic
}


var (
// ErrAppIDNotMatch appid不匹配
ErrAppIDNotMatch = errors.New("app id not match")
Expand All @@ -19,8 +34,8 @@ var (
ErrInvalidPKCS7Padding = errors.New("invalid padding on input")
)

// UserInfo 用户信息
type UserInfo struct {
// PlainData 用户信息/手机号信息
type PlainData struct {
OpenID string `json:"openId"`
UnionID string `json:"unionId"`
NickName string `json:"nickName"`
Expand All @@ -30,18 +45,10 @@ type UserInfo struct {
Country string `json:"country"`
AvatarURL string `json:"avatarUrl"`
Language string `json:"language"`
Watermark struct {
Timestamp int64 `json:"timestamp"`
AppID string `json:"appid"`
} `json:"watermark"`
}

// PhoneInfo 用户手机号
type PhoneInfo struct {
PhoneNumber string `json:"phoneNumber"`
PurePhoneNumber string `json:"purePhoneNumber"`
CountryCode string `json:"countryCode"`
Watermark struct {
Watermark struct {
Timestamp int64 `json:"timestamp"`
AppID string `json:"appid"`
} `json:"watermark"`
Expand Down Expand Up @@ -96,35 +103,18 @@ func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
}

// Decrypt 解密数据
func (basic *Basic) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) {
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
if err != nil {
return nil, err
}
var userInfo UserInfo
err = json.Unmarshal(cipherText, &userInfo)
if err != nil {
return nil, err
}
if userInfo.Watermark.AppID != basic.AppID {
return nil, ErrAppIDNotMatch
}
return &userInfo, nil
}

// DecryptPhone 解密数据(手机)
func (basic *Basic) DecryptPhone(sessionKey, encryptedData, iv string) (*PhoneInfo, error) {
func (encryptor *Encryptor) Decrypt(sessionKey, encryptedData, iv string) (*PlainData, error) {
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
if err != nil {
return nil, err
}
var phoneInfo PhoneInfo
err = json.Unmarshal(cipherText, &phoneInfo)
var plainData PlainData
err = json.Unmarshal(cipherText, &plainData)
if err != nil {
return nil, err
}
if phoneInfo.Watermark.AppID != basic.AppID {
if plainData.Watermark.AppID != encryptor.AppID {
return nil, ErrAppIDNotMatch
}
return &phoneInfo, nil
return &plainData, nil
}
8 changes: 4 additions & 4 deletions miniprogram/miniprogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/silenceper/wechat/v2/credential"
"github.com/silenceper/wechat/v2/miniprogram/analysis"
"github.com/silenceper/wechat/v2/miniprogram/auth"
"github.com/silenceper/wechat/v2/miniprogram/basic"
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
"github.com/silenceper/wechat/v2/miniprogram/config"
"github.com/silenceper/wechat/v2/miniprogram/context"
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
Expand Down Expand Up @@ -36,9 +36,9 @@ func (miniProgram *MiniProgram) GetContext() *context.Context {
return miniProgram.ctx
}

// GetBasic 基础接口(小程序加解密)
func (miniProgram *MiniProgram) GetBasic() *basic.Basic {
return basic.NewBasic(miniProgram.ctx)
// GetEncryptor 小程序加解密
func (miniProgram *MiniProgram) GetEncryptor() *encryptor.Encryptor {
return encryptor.NewEncryptor(miniProgram.ctx)
}

//GetAuth 登录/用户信息相关接口
Expand Down
2 changes: 1 addition & 1 deletion openplatform/miniprogram/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ func (basic *Basic) GetAccountBasicInfo() (*AccountBasicInfo, error) {

//modify_domain设置服务器域名
//TODO
//func (basic *Basic) modifyDomain() {
//func (encryptor *Basic) modifyDomain() {
//}