Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jan 16, 2025
1 parent 2b435f4 commit 675ba5b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 33 deletions.
94 changes: 62 additions & 32 deletions cryptobin/ca/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,46 +50,68 @@ func Test_GenerateKey(t *testing.T) {
obj := New().
SetPublicKeyType("RSA").
WithBits(2048).
GenerateKey().
CreatePrivateKey()
key := obj.ToKeyString()
GenerateKey()

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey")
assertNotEmpty(key, "Test_GenerateKey")
assertNotEmpty(prikey, "Test_GenerateKey-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey-pubkey")
})

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

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey")
assertNotEmpty(key, "Test_GenerateKey")
assertNotEmpty(prikey, "Test_GenerateKey-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey-pubkey")
})

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

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey")
assertNotEmpty(key, "Test_GenerateKey")
assertNotEmpty(prikey, "Test_GenerateKey-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey-pubkey")
})

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

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey")
assertNotEmpty(key, "Test_GenerateKey")
assertNotEmpty(prikey, "Test_GenerateKey-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey-pubkey")
})

t.Run("GenerateRSAKey 2", func(t *testing.T) {
obj := New().
SetGenerateType("RSA").
WithBits(2048).
GenerateKey()

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey")
assertNotEmpty(prikey, "Test_GenerateKey-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey-pubkey")
})

}
Expand All @@ -100,42 +122,50 @@ func Test_GenerateKey2(t *testing.T) {

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

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey2")
assertNotEmpty(key, "Test_GenerateKey2")
assertNotEmpty(prikey, "Test_GenerateKey2-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey2-pubkey")
})

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

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey2")
assertNotEmpty(key, "Test_GenerateKey2")
assertNotEmpty(prikey, "Test_GenerateKey2-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey2-pubkey")
})

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

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey2")
assertNotEmpty(key, "Test_GenerateKey2")
assertNotEmpty(prikey, "Test_GenerateKey2-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey2-pubkey")
})

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

prikey := obj.CreatePrivateKey().ToKeyString()
pubkey := obj.CreatePublicKey().ToKeyString()

assertError(obj.Error(), "Test_GenerateKey2")
assertNotEmpty(key, "Test_GenerateKey2")
assertNotEmpty(prikey, "Test_GenerateKey2-prikey")
assertNotEmpty(pubkey, "Test_GenerateKey2-pubkey")
})

}
41 changes: 41 additions & 0 deletions cryptobin/ca/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ca
import (
"fmt"
"errors"
"crypto/dsa"
"crypto/rsa"
"crypto/ecdsa"
"crypto/ed25519"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/deatil/go-cryptobin/pkcs12"
"github.com/deatil/go-cryptobin/gm/sm2"
cryptobin_x509 "github.com/deatil/go-cryptobin/x509"
pubkey_dsa "github.com/deatil/go-cryptobin/pubkey/dsa"
)

// CA 证书
Expand Down Expand Up @@ -198,6 +200,45 @@ func (this CA) CreatePrivateKey() CA {
return this
}

// Create PublicKey PEM
func (this CA) CreatePublicKey() CA {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

var publicKeyBytes []byte
var err error

switch pubkey := this.publicKey.(type) {
case *rsa.PublicKey:
publicKeyBytes, err = x509.MarshalPKIXPublicKey(pubkey)
case *dsa.PublicKey:
publicKeyBytes, err = pubkey_dsa.MarshalPKCS8PublicKey(pubkey)
case *ecdsa.PublicKey:
publicKeyBytes, err = x509.MarshalPKIXPublicKey(pubkey)
case ed25519.PublicKey:
publicKeyBytes, err = x509.MarshalPKIXPublicKey(pubkey)
case *sm2.PublicKey:
publicKeyBytes, err = sm2.MarshalPublicKey(pubkey)
default:
err = errors.New("privateKey error.")
}

if err != nil {
return this.AppendError(err)
}

publicBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}

this.keyData = pem.EncodeToMemory(publicBlock)

return this
}

// =======================

// pkcs12 密钥
Expand Down
7 changes: 7 additions & 0 deletions cryptobin/ca/with.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func (this CA) SetPublicKeyType(keyType string) CA {
return this
}

// set Generate public key type
// params:
// [ RSA | DSA | ECDSA | EdDSA | SM2 ]
func (this CA) SetGenerateType(typ string) CA {
return this.SetPublicKeyType(typ)
}

// With DSA ParameterSizes
func (this CA) WithParameterSizes(sizes dsa.ParameterSizes) CA {
this.options.ParameterSizes = sizes
Expand Down
3 changes: 3 additions & 0 deletions cryptobin/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ func Test_With(t *testing.T) {
tmp = newSSH.SetPublicKeyType("ECDSA")
assertEqual(tmp.options.PublicKeyType, KeyTypeECDSA, "Test_Get-SetPublicKeyType")

tmp = newSSH.SetGenerateType("EdDSA")
assertEqual(tmp.options.PublicKeyType, KeyTypeEdDSA, "Test_Get-SetGenerateType")

tmp = newSSH.WithCipherName("test-CipherName")
assertEqual(tmp.options.CipherName, "test-CipherName", "Test_Get-WithCipherName")

Expand Down
9 changes: 8 additions & 1 deletion cryptobin/ssh/with.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (this SSH) WithPublicKeyType(keyType PublicKeyType) SSH {
return this
}

// public key type
// set public key type
// params:
// [ RSA | DSA | ECDSA | EdDSA | SM2 ]
func (this SSH) SetPublicKeyType(keyType string) SSH {
Expand All @@ -77,6 +77,13 @@ func (this SSH) SetPublicKeyType(keyType string) SSH {
return this
}

// set Generate public key type
// params:
// [ RSA | DSA | ECDSA | EdDSA | SM2 ]
func (this SSH) SetGenerateType(typ string) SSH {
return this.SetPublicKeyType(typ)
}

// With CipherName
func (this SSH) WithCipherName(cipherName string) SSH {
this.options.CipherName = cipherName
Expand Down

0 comments on commit 675ba5b

Please sign in to comment.