Skip to content

Commit

Permalink
Merge pull request smartwalle#13 from wusphinx/master
Browse files Browse the repository at this point in the history
�依赖最小化
  • Loading branch information
smartwalle authored Jun 7, 2017
2 parents a9e79b9 + ea6bd5b commit 7f7fb64
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 4 deletions.
2 changes: 1 addition & 1 deletion alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"time"

"github.com/smartwalle/going/encoding"
"github.com/smartwalle/alipay/encoding"
)

type AliPay struct {
Expand Down
128 changes: 128 additions & 0 deletions encoding/rsa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package encoding

import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
)

func packageData(originalData []byte, packageSize int) (r [][]byte) {
var src = make([]byte, len(originalData))
copy(src, originalData)

r = make([][]byte, 0)
if len(src) <= packageSize {
return append(r, src)
}
for len(src) > 0 {
var p = src[:packageSize]
r = append(r, p)
src = src[packageSize:]
if len(src) <= packageSize {
r = append(r, src)
break
}
}
return r
}

func RSAEncrypt(plaintext, key []byte) ([]byte, error) {
var err error
var block *pem.Block
block, _ = pem.Decode(key)
if block == nil {
return nil, errors.New("public key error")
}

var pubInterface interface{}
pubInterface, err = x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
var pub = pubInterface.(*rsa.PublicKey)

var data = packageData(plaintext, pub.N.BitLen()/8-11)
var cipherData []byte = make([]byte, 0, 0)

for _, d := range data {
var c, e = rsa.EncryptPKCS1v15(rand.Reader, pub, d)
if e != nil {
return nil, e
}
cipherData = append(cipherData, c...)
}

return cipherData, nil
}

func RSADecrypt(ciphertext, key []byte) ([]byte, error) {
var err error
var block *pem.Block
block, _ = pem.Decode(key)
if block == nil {
return nil, errors.New("private key error")
}

var pri *rsa.PrivateKey
pri, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}

var data = packageData(ciphertext, pri.PublicKey.N.BitLen()/8)
var plainData []byte = make([]byte, 0, 0)

for _, d := range data {
var p, e = rsa.DecryptPKCS1v15(rand.Reader, pri, d)
if e != nil {
return nil, e
}
plainData = append(plainData, p...)
}
return plainData, nil
}

func SignPKCS1v15(src, key []byte, hash crypto.Hash) ([]byte, error) {
var h = hash.New()
h.Write(src)
var hashed = h.Sum(nil)

var err error
var block *pem.Block
block, _ = pem.Decode(key)
if block == nil {
return nil, errors.New("private key error")
}

var pri *rsa.PrivateKey
pri, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa.SignPKCS1v15(rand.Reader, pri, hash, hashed)
}

func VerifyPKCS1v15(src, sig, key []byte, hash crypto.Hash) error {
var h = hash.New()
h.Write(src)
var hashed = h.Sum(nil)

var err error
var block *pem.Block
block, _ = pem.Decode(key)
if block == nil {
return errors.New("publick key error")
}

var pubInterface interface{}
pubInterface, err = x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
var pub = pubInterface.(*rsa.PublicKey)

return rsa.VerifyPKCS1v15(pub, hash, hashed, sig)
}
24 changes: 21 additions & 3 deletions notify.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
package alipay

import (
"io"
"io/ioutil"
"net/http"
"net/url"

"github.com/smartwalle/going/request"
"strings"
)

func NewRequest(method, url string, params url.Values) (*http.Request, error) {
var m = strings.ToUpper(method)
var body io.Reader
if m == "GET" || m == "HEAD" {
if len(params) > 0 {
if strings.Contains(url, "?") {
url = url + "&" + params.Encode()
} else {
url = url + "?" + params.Encode()
}
}
} else {
body = strings.NewReader(params.Encode())
}
return http.NewRequest(m, url, body)
}

func (this *AliPay) NotifyVerify(notifyId string) bool {
var param = url.Values{}
param.Add("service", "notify_verify")
param.Add("partner", this.partnerId)
param.Add("notify_id", notifyId)
req, err := request.NewRequest("GET", this.apiDomain, param)
req, err := NewRequest("GET", this.apiDomain, param)
if err != nil {
return false
}
Expand Down

0 comments on commit 7f7fb64

Please sign in to comment.