forked from smartwalle/alipay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request smartwalle#13 from wusphinx/master
�依赖最小化
- Loading branch information
Showing
3 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters