Skip to content

Commit

Permalink
ecc & rsa enhance
Browse files Browse the repository at this point in the history
  • Loading branch information
AbericYang committed May 27, 2020
1 parent fbf1d27 commit 099e0aa
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
7 changes: 2 additions & 5 deletions ecc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ func ECCGenerate(curve elliptic.Curve) (*ecdsa.PrivateKey, *ecdsa.PublicKey, err
//
// curve 曲线生成类型,如 crypto.S256()/elliptic.P256()/elliptic.P384()/elliptic.P512()
func ECCGeneratePemBytes(priPemType, pubPemType, passwd string, curve elliptic.Curve) (priBytes, pubBytes []byte, err error) {
var (
privateKey *ecdsa.PrivateKey
publicKey *ecdsa.PublicKey
)
var privateKey *ecdsa.PrivateKey
if privateKey, err = ecdsa.GenerateKey(curve, rand.Reader); nil == err {
publicKey = &privateKey.PublicKey
publicKey := &privateKey.PublicKey
if priBytes, err = ECCPri2PemBytes(priPemType, passwd, privateKey); nil != err {
return
}
Expand Down
66 changes: 66 additions & 0 deletions rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,72 @@ func RSAGenerateKey(bits int, path, priFileName, pubFileName, priPemType, pubPem
return RSAGenerateKeyWithPass(bits, path, priFileName, pubFileName, "", priPemType, pubPemType, -1, pks)
}

// RSAGenerateKeyPemBytes RSA公钥私钥产生
//
// bits 指定生成位大小
//
// path 指定公私钥所在生成目录
//
// pks 私钥格式,默认提供PKCS1和PKCS8,通过调用‘CryptoRSA().PKSC1()’和‘CryptoRSA().PKSC8()’方法赋值
func RSAGenerateKeyPemBytes(bits int, priPemType, pubPemType, passwd string, pks PKSCType) (priBytes, pubBytes []byte, err error) {
var privateKey *rsa.PrivateKey
// 生成私钥文件
if privateKey, err = rsa.GenerateKey(rand.Reader, bits); nil == err {
publicKey := &privateKey.PublicKey
if priBytes, err = RSAPri2PemBytes(priPemType, passwd, privateKey, pks); nil != err {
return
}
if pubBytes, err = RSAPub2PemBytes(pubPemType, publicKey); nil != err {
return
}
}
return
}

// RSAPri2PemBytes RSAPri2PemBytes
func RSAPri2PemBytes(priPemType, passwd string, privateKey *rsa.PrivateKey, pks PKSCType) (data []byte, err error) {
var (
derStream []byte
block *pem.Block
)
switch pks {
default:
derStream = x509.MarshalPKCS1PrivateKey(privateKey)
case pksC8:
if derStream, err = x509.MarshalPKCS8PrivateKey(privateKey); nil != err {
return
}
}
// block表示PEM编码的结构
if StringIsEmpty(passwd) {
block = &pem.Block{Type: priPemType, Bytes: derStream}
} else {
if block, err = x509.EncryptPEMBlock(rand.Reader, priPemType, derStream, []byte(passwd), x509.PEMCipher3DES); nil != err {
return
}
}
data = pem.EncodeToMemory(block)
return
}

// RSAPub2PemBytes RSAPub2PemBytes
func RSAPub2PemBytes(pubPemType string, publicKey *rsa.PublicKey) (data []byte, err error) {
var (
derPkiX []byte
block *pem.Block
)
// 将公钥序列化为der编码的PKIX格式
if derPkiX, err = x509.MarshalPKIXPublicKey(publicKey); nil == err {
// block表示PEM编码的结构
block = &pem.Block{
Type: pubPemType,
Bytes: derPkiX,
}
data = pem.EncodeToMemory(block)
}
return
}

// RSAGenerateKeyWithPass RSA公钥私钥产生
//
// bits 指定生成位大小
Expand Down
8 changes: 8 additions & 0 deletions rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/hex"
"gotest.tools/assert"
"io/ioutil"
"path/filepath"
"testing"
Expand Down Expand Up @@ -60,6 +61,13 @@ func TestRSACommon_GenerateRsaKey(t *testing.T) {
t.Log(RSAGenerateKey(2048, pathrsapksc82048, privateRSAName, publicRSAName, "PRIVATE KEY", "PUBLIC KEY", RSAPKSC8()))
}

func TestRSAGenerateKeyPemBytes(t *testing.T) {
priBytes, pubBytes, err := RSAGenerateKeyPemBytes(2048, "PRIVATE KEY", "PUBLIC KEY", "", RSAPKSC1())
assert.NilError(t, err)
t.Log(string(priBytes))
t.Log(string(pubBytes))
}

func TestRSACommon_GenerateRsaCustomPriKey(t *testing.T) {
t.Log(RSAGeneratePriKeyWithPass(256, pathrsapksc1256, "private1.pem", "123456", "PRIVATE KEY", x509.PEMCipher3DES, RSAPKSC1()))
t.Log(RSAGeneratePriKey(512, pathrsapksc1512, "private1.pem", "PUBLIC KEY", RSAPKSC1()))
Expand Down

0 comments on commit 099e0aa

Please sign in to comment.