From 54e2c82fff17fd7dfbd1a5fade54bacaef0caabe Mon Sep 17 00:00:00 2001 From: cielu Date: Sat, 12 Oct 2019 16:03:39 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/decrypt.go | 43 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/miniprogram/decrypt.go b/miniprogram/decrypt.go index 6897368..fce7775 100644 --- a/miniprogram/decrypt.go +++ b/miniprogram/decrypt.go @@ -36,6 +36,17 @@ type UserInfo struct { } `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 func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { if blockSize <= 0 { @@ -57,8 +68,8 @@ func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { return data[:len(data)-n], nil } -// Decrypt 解密数据 -func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) { +// get cipherText +func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) { aesKey, err := base64.StdEncoding.DecodeString(sessionKey) if err != nil { return nil, err @@ -81,6 +92,16 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo if err != nil { 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 err = json.Unmarshal(cipherText, &userInfo) if err != nil { @@ -91,3 +112,21 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo } 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 +} From bb97bddc08c0cfd9ca7d28c86e11949bc98225d2 Mon Sep 17 00:00:00 2001 From: ciel yu Date: Sat, 12 Oct 2019 16:27:55 +0800 Subject: [PATCH 2/5] =?UTF-8?q?Revert=20"=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E4=B9=8B=E5=89=8D=E7=9A=84=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E8=A7=A3=E5=AF=86"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 54e2c82f --- miniprogram/decrypt.go | 43 ++---------------------------------------- 1 file changed, 2 insertions(+), 41 deletions(-) diff --git a/miniprogram/decrypt.go b/miniprogram/decrypt.go index fce7775..6897368 100644 --- a/miniprogram/decrypt.go +++ b/miniprogram/decrypt.go @@ -36,17 +36,6 @@ type UserInfo struct { } `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 func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { if blockSize <= 0 { @@ -68,8 +57,8 @@ func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { return data[:len(data)-n], nil } -// get cipherText -func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) { +// Decrypt 解密数据 +func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) { aesKey, err := base64.StdEncoding.DecodeString(sessionKey) if err != nil { return nil, err @@ -92,16 +81,6 @@ func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) { if err != nil { 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 err = json.Unmarshal(cipherText, &userInfo) if err != nil { @@ -112,21 +91,3 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo } 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 -} From 453089e83e315fcf0943cfcccf68a471e3d19d3b Mon Sep 17 00:00:00 2001 From: ciel yu Date: Sat, 12 Oct 2019 16:41:26 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/decrypt.go | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/miniprogram/decrypt.go b/miniprogram/decrypt.go index 6897368..bcf6a94 100644 --- a/miniprogram/decrypt.go +++ b/miniprogram/decrypt.go @@ -36,6 +36,17 @@ type UserInfo struct { } `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 func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { if blockSize <= 0 { @@ -57,8 +68,8 @@ func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { return data[:len(data)-n], nil } -// Decrypt 解密数据 -func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) { +// get cipherText +func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) { aesKey, err := base64.StdEncoding.DecodeString(sessionKey) if err != nil { return nil, err @@ -81,6 +92,15 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo if err != nil { return nil, err } + return cipherText, nil +} + +// Decrypt 解密数据(用户) +func (wxa *MiniProgram) 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 { @@ -91,3 +111,20 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo } return &userInfo, nil } + +// DecryptPhone 解密数据(手机) +func (wxa *MiniProgram) DecryptPHone(sessionKey, encryptedData, iv string) (*PhoneInfo, error) { + 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 +} From 4e6fd625da22495d077a7f77bef8c61c274646ca Mon Sep 17 00:00:00 2001 From: ciel yu Date: Sat, 12 Oct 2019 16:46:59 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/decrypt.go | 46 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/miniprogram/decrypt.go b/miniprogram/decrypt.go index bcf6a94..87a4202 100644 --- a/miniprogram/decrypt.go +++ b/miniprogram/decrypt.go @@ -47,27 +47,6 @@ type PhoneInfo struct { } `json:"watermark"` } -// pkcs7Unpad returns slice of the original data without padding -func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { - if blockSize <= 0 { - return nil, ErrInvalidBlockSize - } - if len(data)%blockSize != 0 || len(data) == 0 { - return nil, ErrInvalidPKCS7Data - } - c := data[len(data)-1] - n := int(c) - if n == 0 || n > len(data) { - return nil, ErrInvalidPKCS7Padding - } - for i := 0; i < n; i++ { - if data[len(data)-n+i] != c { - return nil, ErrInvalidPKCS7Padding - } - } - return data[:len(data)-n], nil -} - // get cipherText func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) { aesKey, err := base64.StdEncoding.DecodeString(sessionKey) @@ -95,7 +74,28 @@ func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) { return cipherText, nil } -// Decrypt 解密数据(用户) +// pkcs7Unpad returns slice of the original data without padding +func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { + if blockSize <= 0 { + return nil, ErrInvalidBlockSize + } + if len(data)%blockSize != 0 || len(data) == 0 { + return nil, ErrInvalidPKCS7Data + } + c := data[len(data)-1] + n := int(c) + if n == 0 || n > len(data) { + return nil, ErrInvalidPKCS7Padding + } + for i := 0; i < n; i++ { + if data[len(data)-n+i] != c { + return nil, ErrInvalidPKCS7Padding + } + } + return data[:len(data)-n], nil +} + +// Decrypt 解密数据 func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) { cipherText, err := getCipherText(sessionKey, encryptedData, iv) if err != nil { @@ -113,7 +113,7 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo } // DecryptPhone 解密数据(手机) -func (wxa *MiniProgram) DecryptPHone(sessionKey, encryptedData, iv string) (*PhoneInfo, error) { +func (wxa *MiniProgram) DecryptPhone(sessionKey, encryptedData, iv string) (*PhoneInfo, error) { cipherText, err := getCipherText(sessionKey, encryptedData, iv) if err != nil { return nil, err From 0dffcde4759ec76bb60e7e6e8733a83c08a7f119 Mon Sep 17 00:00:00 2001 From: ciel yu Date: Sat, 12 Oct 2019 17:38:41 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E8=A7=A3=E5=AF=86=EF=BC=8C=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/decrypt.go | 46 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/miniprogram/decrypt.go b/miniprogram/decrypt.go index 87a4202..d904b3c 100644 --- a/miniprogram/decrypt.go +++ b/miniprogram/decrypt.go @@ -36,7 +36,7 @@ type UserInfo struct { } `json:"watermark"` } -// 用户手机号 +// PhoneInfo 用户手机号 type PhoneInfo struct { PhoneNumber string `json:"phoneNumber"` PurePhoneNumber string `json:"purePhoneNumber"` @@ -47,7 +47,28 @@ type PhoneInfo struct { } `json:"watermark"` } -// get cipherText +// pkcs7Unpad returns slice of the original data without padding +func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { + if blockSize <= 0 { + return nil, ErrInvalidBlockSize + } + if len(data)%blockSize != 0 || len(data) == 0 { + return nil, ErrInvalidPKCS7Data + } + c := data[len(data)-1] + n := int(c) + if n == 0 || n > len(data) { + return nil, ErrInvalidPKCS7Padding + } + for i := 0; i < n; i++ { + if data[len(data)-n+i] != c { + return nil, ErrInvalidPKCS7Padding + } + } + return data[:len(data)-n], nil +} + +// getCipherText returns slice of the cipher text func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) { aesKey, err := base64.StdEncoding.DecodeString(sessionKey) if err != nil { @@ -74,27 +95,6 @@ func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) { return cipherText, nil } -// pkcs7Unpad returns slice of the original data without padding -func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { - if blockSize <= 0 { - return nil, ErrInvalidBlockSize - } - if len(data)%blockSize != 0 || len(data) == 0 { - return nil, ErrInvalidPKCS7Data - } - c := data[len(data)-1] - n := int(c) - if n == 0 || n > len(data) { - return nil, ErrInvalidPKCS7Padding - } - for i := 0; i < n; i++ { - if data[len(data)-n+i] != c { - return nil, ErrInvalidPKCS7Padding - } - } - return data[:len(data)-n], nil -} - // Decrypt 解密数据 func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) { cipherText, err := getCipherText(sessionKey, encryptedData, iv)