Use chinese crypto

This commit is contained in:
Liyi Meng
2022-10-08 21:15:47 +02:00
parent 401fafb7e6
commit e425269f0e
4 changed files with 80 additions and 14 deletions

View File

@@ -41,7 +41,7 @@ const (
)
// EncodePublicKeyPEM returns PEM-encoded public data
func EncodePublicKeyPEM(key *rsa.PublicKey) ([]byte, error) {
func EncodePublicKeyPEM(key interface{}) ([]byte, error) {
der, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return []byte{}, err
@@ -54,12 +54,25 @@ func EncodePublicKeyPEM(key *rsa.PublicKey) ([]byte, error) {
}
// EncodePrivateKeyPEM returns PEM-encoded private key data
func EncodePrivateKeyPEM(key *rsa.PrivateKey) []byte {
block := pem.Block{
Type: RSAPrivateKeyBlockType,
Bytes: x509.MarshalPKCS1PrivateKey(key),
func EncodePrivateKeyPEM(key interface{}) []byte {
var block *pem.Block
switch privKey := key.(type) {
case *ecdsa.PrivateKey:
derBytes, _ := x509.MarshalECPrivateKey(privKey)
block = &pem.Block{
Type: ECPrivateKeyBlockType,
Bytes: derBytes,
}
case *rsa.PrivateKey:
block = &pem.Block{
Type: RSAPrivateKeyBlockType,
Bytes: x509.MarshalPKCS1PrivateKey(privKey),
}
}
return pem.EncodeToMemory(&block)
if block != nil {
return pem.EncodeToMemory(block)
}
return []byte{}
}
// EncodeCertPEM returns PEM-endcoded certificate data