You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
2.7 KiB
133 lines
2.7 KiB
package sra
|
|
|
|
/*
|
|
RSA 非对称加密算法封装
|
|
*/
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/sha256"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
//RSA公钥私钥产生
|
|
func GenRsaKey(bits int) (prvkey, pubkey []byte, err error) {
|
|
// 生成私钥文件
|
|
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
|
|
if err != nil {
|
|
return
|
|
}
|
|
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
|
|
block := &pem.Block{
|
|
Type: "RSA PRIVATE KEY",
|
|
Bytes: derStream,
|
|
}
|
|
prvkey = pem.EncodeToMemory(block)
|
|
publicKey := &privateKey.PublicKey
|
|
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
|
|
if err != nil {
|
|
return
|
|
}
|
|
block = &pem.Block{
|
|
Type: "PUBLIC KEY",
|
|
Bytes: derPkix,
|
|
}
|
|
pubkey = pem.EncodeToMemory(block)
|
|
return
|
|
}
|
|
|
|
//签名
|
|
func RsaSignWithSha256(data []byte, keyBytes []byte) (sgin []byte, err error) {
|
|
var (
|
|
privateKey *rsa.PrivateKey
|
|
)
|
|
h := sha256.New()
|
|
h.Write(data)
|
|
hashed := h.Sum(nil)
|
|
block, _ := pem.Decode(keyBytes)
|
|
if block == nil {
|
|
err = errors.New("private key error")
|
|
return
|
|
}
|
|
privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
if err != nil {
|
|
err = fmt.Errorf("ParsePKCS8PrivateKey err:%v", err)
|
|
return
|
|
}
|
|
|
|
sgin, err = rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed)
|
|
if err != nil {
|
|
err = fmt.Errorf("Error from signing err:%v", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
//验证
|
|
func RsaVerySignWithSha256(data, signData, keyBytes []byte) (issucc bool, err error) {
|
|
var (
|
|
pubKey interface{}
|
|
)
|
|
block, _ := pem.Decode(keyBytes)
|
|
if block == nil {
|
|
err = errors.New("public key error")
|
|
return
|
|
}
|
|
pubKey, err = x509.ParsePKIXPublicKey(block.Bytes)
|
|
if err != nil {
|
|
return
|
|
}
|
|
hashed := sha256.Sum256(data)
|
|
err = rsa.VerifyPKCS1v15(pubKey.(*rsa.PublicKey), crypto.SHA256, hashed[:], signData)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// 公钥加密
|
|
func RsaEncrypt(data, keyBytes []byte) ([]byte, error) {
|
|
//解密pem格式的公钥
|
|
block, _ := pem.Decode(keyBytes)
|
|
if block == nil {
|
|
return nil, errors.New("public key error")
|
|
}
|
|
// 解析公钥
|
|
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// 类型断言
|
|
pub := pubInterface.(*rsa.PublicKey)
|
|
//加密
|
|
ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, pub, data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ciphertext, nil
|
|
}
|
|
|
|
// 私钥解密
|
|
func RsaDecrypt(ciphertext, keyBytes []byte) ([]byte, error) {
|
|
//获取私钥
|
|
block, _ := pem.Decode(keyBytes)
|
|
if block == nil {
|
|
return nil, errors.New("private key error!")
|
|
}
|
|
//解析PKCS1格式的私钥
|
|
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// 解密
|
|
data, err := rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|