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 0b4bc91 commit 2b435f4
Show file tree
Hide file tree
Showing 6 changed files with 620 additions and 38 deletions.
58 changes: 58 additions & 0 deletions cryptobin/ca/ca.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
package ca

import (
"strconv"
"crypto"
"crypto/dsa"
"crypto/elliptic"
)

// public key type
type PublicKeyType uint

func (typ PublicKeyType) String() string {
switch typ {
case KeyTypeUnknown:
return "Unknown"
case KeyTypeRSA:
return "RSA"
case KeyTypeDSA:
return "DSA"
case KeyTypeECDSA:
return "ECDSA"
case KeyTypeEdDSA:
return "EdDSA"
case KeyTypeSM2:
return "SM2"
default:
return "unknown KeyType value " + strconv.Itoa(int(typ))
}
}

const (
KeyTypeUnknown PublicKeyType = iota
KeyTypeRSA
KeyTypeDSA
KeyTypeECDSA
KeyTypeEdDSA
KeyTypeSM2
)

// Options
type Options struct {
// public key type
PublicKeyType PublicKeyType

// DSA ParameterSizes
ParameterSizes dsa.ParameterSizes

// ecc curve
Curve elliptic.Curve

// generates RSA private key bit size
Bits int
}

/**
* CA
*
Expand All @@ -27,6 +76,9 @@ type CA struct {
// 可用 [*rsa.PublicKey | *ecdsa.PublicKey | ed25519.PublicKey | *sm2.PublicKey]
publicKey crypto.PublicKey

// options
options Options

// [私钥/公钥/cert]数据
keyData []byte

Expand All @@ -37,6 +89,12 @@ type CA struct {
// 构造函数
func NewCA() CA {
return CA{
options: Options{
PublicKeyType: KeyTypeRSA,
ParameterSizes: dsa.L1024N160,
Curve: elliptic.P256(),
Bits: 2048,
},
Errors: make([]error, 0),
}
}
Expand Down
74 changes: 64 additions & 10 deletions cryptobin/ca/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func Test_CreateCA(t *testing.T) {
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)

obj := New().
GenerateRSAKey(512).
SetPublicKeyType("RSA").
WithBits(512).
GenerateKey().
MakeCA(pkix.Name{
CommonName: "test.example.com",
Organization: []string{"Test"},
Expand All @@ -40,7 +42,59 @@ func Test_CreateCA(t *testing.T) {
}
}

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

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

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

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

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

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

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

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

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

}

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

Expand All @@ -50,8 +104,8 @@ func Test_CreatePrivateKey_RSA(t *testing.T) {
CreatePrivateKey()
key := obj.ToKeyString()

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

t.Run("GenerateECDSAKey", func(t *testing.T) {
Expand All @@ -60,8 +114,8 @@ func Test_CreatePrivateKey_RSA(t *testing.T) {
CreatePrivateKey()
key := obj.ToKeyString()

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

t.Run("GenerateEdDSAKey", func(t *testing.T) {
Expand All @@ -70,8 +124,8 @@ func Test_CreatePrivateKey_RSA(t *testing.T) {
CreatePrivateKey()
key := obj.ToKeyString()

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

t.Run("GenerateSM2Key", func(t *testing.T) {
Expand All @@ -80,8 +134,8 @@ func Test_CreatePrivateKey_RSA(t *testing.T) {
CreatePrivateKey()
key := obj.ToKeyString()

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

}
Loading

0 comments on commit 2b435f4

Please sign in to comment.