【新增】私有ca

This commit is contained in:
v-me-50
2025-08-26 16:52:07 +08:00
parent e53a88e4dd
commit c9cb3fa5e1
10 changed files with 1520 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package private_ca
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"fmt"
gm "github.com/tjfoc/gmsm/sm2"
)
func generatePrivateKey(keyType KeyType, keyBits int) (interface{}, error) {
switch keyType {
case KeyRSA:
if keyBits == 0 {
keyBits = 2048
}
return rsa.GenerateKey(rand.Reader, keyBits)
case KeyECDSA:
var c elliptic.Curve
switch keyBits {
case 224:
c = elliptic.P224()
case 256:
c = elliptic.P256()
case 384:
c = elliptic.P384()
case 521:
c = elliptic.P521()
default:
c = elliptic.P384()
}
return ecdsa.GenerateKey(c, rand.Reader)
case KeySM2:
return gm.GenerateKey(rand.Reader)
default:
return nil, fmt.Errorf("unsupported key type: %s", keyType)
}
}