-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
176 additions
and
95 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,88 @@ | ||
package codec | ||
|
||
import ( | ||
"bytes" | ||
"crypto" | ||
"crypto/md5" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/sha1" | ||
"crypto/sha256" | ||
"io/ioutil" | ||
) | ||
|
||
// 设置私钥 | ||
func NewRSAPrivateKey(priStr string) (r *RSAPrivateKey, err error) { | ||
r = &RSAPrivateKey{} | ||
r.priStr = priStr | ||
r.prikey, err = r.GetPrivatekey() | ||
return | ||
} | ||
|
||
type RSAPrivateKey struct { | ||
priStr string //私钥字符串 | ||
prikey *rsa.PrivateKey //私钥 | ||
} | ||
|
||
// *rsa.PublicKey | ||
func (r *RSAPrivateKey) GetPrivatekey() (*rsa.PrivateKey, error) { | ||
return getPriKey([]byte(r.priStr)) | ||
} | ||
|
||
// 私钥加密 | ||
func (rsas *RSAPrivateKey) Encrypt(input []byte) ([]byte, error) { | ||
if rsas.prikey == nil { | ||
return nil, ErrPrivateKeyNotSet | ||
} | ||
output := bytes.NewBuffer(nil) | ||
err := priKeyIO(rsas.prikey, bytes.NewReader(input), output, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ioutil.ReadAll(output) | ||
} | ||
|
||
// 私钥解密 | ||
func (r *RSAPrivateKey) Decrypt(input []byte) ([]byte, error) { | ||
if r.prikey == nil { | ||
return nil, ErrPrivateKeyNotSet | ||
} | ||
output := bytes.NewBuffer(nil) | ||
err := priKeyIO(r.prikey, bytes.NewReader(input), output, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ioutil.ReadAll(output) | ||
} | ||
|
||
/** | ||
* 使用RSAWithMD5算法签名 | ||
*/ | ||
func (r *RSAPrivateKey) SignMd5(data []byte) ([]byte, error) { | ||
md5Hash := md5.New() | ||
md5Hash.Write(data) | ||
hashed := md5Hash.Sum(nil) | ||
return rsa.SignPKCS1v15(rand.Reader, r.prikey, crypto.MD5, hashed) | ||
} | ||
|
||
/** | ||
* 使用RSAWithSHA1算法签名 | ||
*/ | ||
func (r *RSAPrivateKey) SignSha1(data []byte) ([]byte, error) { | ||
sha1Hash := sha1.New() | ||
sha1Hash.Write(data) | ||
hashed := sha1Hash.Sum(nil) | ||
return rsa.SignPKCS1v15(rand.Reader, r.prikey, crypto.SHA1, hashed) | ||
} | ||
|
||
/** | ||
* 使用RSAWithSHA256算法签名 | ||
*/ | ||
func (r *RSAPrivateKey) SignSha256(data []byte) ([]byte, error) { | ||
sha256Hash := sha256.New() | ||
s_data := []byte(data) | ||
sha256Hash.Write(s_data) | ||
hashed := sha256Hash.Sum(nil) | ||
return rsa.SignPKCS1v15(rand.Reader, r.prikey, crypto.SHA256, hashed) | ||
} |
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,88 @@ | ||
package codec | ||
|
||
import ( | ||
"bytes" | ||
"crypto" | ||
"crypto/md5" | ||
"crypto/rsa" | ||
"crypto/sha1" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"io/ioutil" | ||
) | ||
|
||
// 设置公钥 | ||
func NewRSAPublicKey(pubStr string) (r *RSAPublicKey, err error) { | ||
r = &RSAPublicKey{} | ||
r.pubStr = pubStr | ||
r.pubkey, err = r.GetPublickey() | ||
return | ||
} | ||
|
||
type RSAPublicKey struct { | ||
pubStr string //公钥字符串 | ||
pubkey *rsa.PublicKey //公钥 | ||
} | ||
|
||
// *rsa.PrivateKey | ||
func (r *RSAPublicKey) GetPublickey() (*rsa.PublicKey, error) { | ||
return getPubKey([]byte(r.pubStr)) | ||
} | ||
|
||
// 公钥加密 | ||
func (r *RSAPublicKey) Encrypt(input []byte) ([]byte, error) { | ||
if r.pubkey == nil { | ||
return nil, ErrPublicKeyNotSet | ||
} | ||
output := bytes.NewBuffer(nil) | ||
err := pubKeyIO(r.pubkey, bytes.NewReader(input), output, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ioutil.ReadAll(output) | ||
} | ||
|
||
// 公钥解密 | ||
func (r *RSAPublicKey) Decrypt(input []byte) ([]byte, error) { | ||
if r.pubkey == nil { | ||
return nil, ErrPublicKeyNotSet | ||
} | ||
output := bytes.NewBuffer(nil) | ||
err := pubKeyIO(r.pubkey, bytes.NewReader(input), output, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return ioutil.ReadAll(output) | ||
} | ||
|
||
/** | ||
* 使用RSAWithMD5验证签名 | ||
*/ | ||
func (r *RSAPublicKey) VerifySignMd5(data string, signData string) error { | ||
sign, err := base64.StdEncoding.DecodeString(signData) | ||
if err != nil { | ||
return err | ||
} | ||
hash := md5.New() | ||
hash.Write([]byte(data)) | ||
return rsa.VerifyPKCS1v15(r.pubkey, crypto.MD5, hash.Sum(nil), sign) | ||
} | ||
|
||
/** | ||
* 使用RSAWithSHA1验证签名 | ||
*/ | ||
func (r *RSAPublicKey) VerifySignSha1(data []byte, sign []byte) error { | ||
hash := sha1.New() | ||
hash.Write(data) | ||
return rsa.VerifyPKCS1v15(r.pubkey, crypto.SHA1, hash.Sum(nil), sign) | ||
} | ||
|
||
/** | ||
* 使用RSAWithSHA256验证签名 | ||
*/ | ||
func (r *RSAPublicKey) VerifySignSha256(data []byte, sign []byte) error { | ||
hash := sha256.New() | ||
hash.Write(data) | ||
|
||
return rsa.VerifyPKCS1v15(r.pubkey, crypto.SHA256, hash.Sum(nil), sign) | ||
} |