Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jan 15, 2025
1 parent 3abc12e commit 0b4bc91
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 51 deletions.
8 changes: 6 additions & 2 deletions cryptobin/ca/ca.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ca

import (
"crypto"
)

/**
* CA
*
Expand All @@ -17,11 +21,11 @@ type CA struct {

// 私钥
// 可用 [*rsa.PrivateKey | *ecdsa.PrivateKey | ed25519.PrivateKey | *sm2.PrivateKey]
privateKey any
privateKey crypto.PrivateKey

// 公钥
// 可用 [*rsa.PublicKey | *ecdsa.PublicKey | ed25519.PublicKey | *sm2.PublicKey]
publicKey any
publicKey crypto.PublicKey

// [私钥/公钥/cert]数据
keyData []byte
Expand Down
46 changes: 46 additions & 0 deletions cryptobin/ca/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,49 @@ func Test_CreateCA(t *testing.T) {
t.Fatal(err)
}
}

func Test_CreatePrivateKey_RSA(t *testing.T) {
assertError := cryptobin_test.AssertErrorT(t)
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)

t.Run("GenerateRSAKey", func(t *testing.T) {
obj := New().
GenerateRSAKey(2048).
CreatePrivateKey()
key := obj.ToKeyString()

assertError(obj.Error(), "Test_CreatePrivateKey_RSA")
assertNotEmpty(key, "Test_CreatePrivateKey_RSA")
})

t.Run("GenerateECDSAKey", func(t *testing.T) {
obj := New().
GenerateECDSAKey("P256").
CreatePrivateKey()
key := obj.ToKeyString()

assertError(obj.Error(), "Test_CreatePrivateKey_RSA")
assertNotEmpty(key, "Test_CreatePrivateKey_RSA")
})

t.Run("GenerateEdDSAKey", func(t *testing.T) {
obj := New().
GenerateEdDSAKey().
CreatePrivateKey()
key := obj.ToKeyString()

assertError(obj.Error(), "Test_CreatePrivateKey_RSA")
assertNotEmpty(key, "Test_CreatePrivateKey_RSA")
})

t.Run("GenerateSM2Key", func(t *testing.T) {
obj := New().
GenerateSM2Key().
CreatePrivateKey()
key := obj.ToKeyString()

assertError(obj.Error(), "Test_CreatePrivateKey_RSA")
assertNotEmpty(key, "Test_CreatePrivateKey_RSA")
})

}
54 changes: 15 additions & 39 deletions cryptobin/ca/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,53 +168,29 @@ func (this CA) CreatePrivateKey() CA {
return this.AppendError(err)
}

var privateBlock *pem.Block
var privateKeyBytes []byte
var err error

switch privateKey := this.privateKey.(type) {
case *rsa.PrivateKey:
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)

privateBlock = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyBytes,
}

privateKeyBytes, err = x509.MarshalPKCS8PrivateKey(privateKey)
case *ecdsa.PrivateKey:
privateKeyBytes, err := x509.MarshalECPrivateKey(privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock = &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: privateKeyBytes,
}

privateKeyBytes, err = x509.MarshalPKCS8PrivateKey(privateKey)
case ed25519.PrivateKey:
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return this.AppendError(err)
}

privateBlock = &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
}

privateKeyBytes, err = x509.MarshalPKCS8PrivateKey(privateKey)
case *sm2.PrivateKey:
privateKeyBytes, err := sm2.MarshalPrivateKey(privateKey)
if err != nil {
return this.AppendError(err)
}
privateKeyBytes, err = sm2.MarshalPrivateKey(privateKey)
default:
err = fmt.Errorf("unsupported private key type: %T", privateKey)
}

privateBlock = &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
}
if err != nil {
return this.AppendError(err)
}

default:
err := fmt.Errorf("unsupported private key type: %T", privateKey)
return this.AppendError(err)
privateBlock := &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateKeyBytes,
}

this.keyData = pem.EncodeToMemory(privateBlock)
Expand Down
8 changes: 6 additions & 2 deletions cryptobin/ca/get.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ca

import (
"crypto"
)

// 获取 cert
func (this CA) GetCert() any {
return this.cert
Expand All @@ -11,12 +15,12 @@ func (this CA) GetCertRequest() any {
}

// 获取 PrivateKey
func (this CA) GetPrivateKey() any {
func (this CA) GetPrivateKey() crypto.PrivateKey {
return this.privateKey
}

// 获取 publicKey
func (this CA) GetPublicKey() any {
func (this CA) GetPublicKey() crypto.PublicKey {
return this.publicKey
}

Expand Down
20 changes: 12 additions & 8 deletions cryptobin/ca/with.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
package ca

import (
"crypto"
)

// 设置 cert
// 可用 [*x509.Certificate | *sm2X509.Certificate]
func (this CA) WithCert(data any) CA {
this.cert = data
func (this CA) WithCert(cert any) CA {
this.cert = cert

return this
}

// 设置 certRequest
// 可用 [*x509.CertificateRequest | *sm2X509.CertificateRequest]
func (this CA) WithCertRequest(data any) CA {
this.certRequest = data
func (this CA) WithCertRequest(cert any) CA {
this.certRequest = cert

return this
}

// 设置 PrivateKey
func (this CA) WithPrivateKey(data any) CA {
this.privateKey = data
func (this CA) WithPrivateKey(key crypto.PrivateKey) CA {
this.privateKey = key

return this
}

// 设置 publicKey
func (this CA) WithPublicKey(data any) CA {
this.publicKey = data
func (this CA) WithPublicKey(key crypto.PublicKey) CA {
this.publicKey = key

return this
}
Expand Down

0 comments on commit 0b4bc91

Please sign in to comment.