mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 20:07:38 +03:00
23 lines
563 B
Go
23 lines
563 B
Go
package network
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
)
|
|
|
|
func PEMBlock(data interface{}) *pem.Block {
|
|
var pemBlock *pem.Block
|
|
switch key := data.(type) {
|
|
case *ecdsa.PrivateKey:
|
|
keyBytes, _ := x509.MarshalECPrivateKey(key)
|
|
pemBlock = &pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes}
|
|
case *rsa.PrivateKey:
|
|
pemBlock = &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}
|
|
case *x509.CertificateRequest:
|
|
pemBlock = &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: key.Raw}
|
|
}
|
|
|
|
return pemBlock
|
|
}
|