mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 21:02:25 +08:00
小程序数据解密,增加手机号解密,兼容之前的用户信息解密
This commit is contained in:
@@ -36,6 +36,17 @@ type UserInfo struct {
|
|||||||
} `json:"watermark"`
|
} `json:"watermark"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 用户手机号
|
||||||
|
type PhoneInfo struct {
|
||||||
|
PhoneNumber string `json:"phoneNumber"`
|
||||||
|
PurePhoneNumber string `json:"purePhoneNumber"`
|
||||||
|
CountryCode string `json:"countryCode"`
|
||||||
|
Watermark struct {
|
||||||
|
Timestamp int64 `json:"timestamp"`
|
||||||
|
AppID string `json:"appid"`
|
||||||
|
} `json:"watermark"`
|
||||||
|
}
|
||||||
|
|
||||||
// pkcs7Unpad returns slice of the original data without padding
|
// pkcs7Unpad returns slice of the original data without padding
|
||||||
func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
|
func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
|
||||||
if blockSize <= 0 {
|
if blockSize <= 0 {
|
||||||
@@ -57,8 +68,8 @@ func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
|
|||||||
return data[:len(data)-n], nil
|
return data[:len(data)-n], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrypt 解密数据
|
// get cipherText
|
||||||
func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) {
|
func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
|
||||||
aesKey, err := base64.StdEncoding.DecodeString(sessionKey)
|
aesKey, err := base64.StdEncoding.DecodeString(sessionKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -81,6 +92,16 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return cipherText, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt 解密(用户)数据
|
||||||
|
func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) {
|
||||||
|
// 拿到 cipherText
|
||||||
|
cipherText,err := getCipherText(sessionKey, encryptedData, iv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
var userInfo UserInfo
|
var userInfo UserInfo
|
||||||
err = json.Unmarshal(cipherText, &userInfo)
|
err = json.Unmarshal(cipherText, &userInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -91,3 +112,21 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo
|
|||||||
}
|
}
|
||||||
return &userInfo, nil
|
return &userInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decrypt 解密(手机)数据
|
||||||
|
func (wxa *MiniProgram) DecryptPhone(sessionKey, encryptedData, iv string) (*PhoneInfo, error) {
|
||||||
|
// 拿到 cipherText
|
||||||
|
cipherText,err := getCipherText(sessionKey, encryptedData, iv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var phoneInfo PhoneInfo
|
||||||
|
err = json.Unmarshal(cipherText, &phoneInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if phoneInfo.Watermark.AppID != wxa.AppID {
|
||||||
|
return nil, ErrAppIDNotMatch
|
||||||
|
}
|
||||||
|
return &phoneInfo, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user