mirror of
https://github.com/usual2970/certimate.git
synced 2025-09-20 23:16:00 +00:00
.github
.vscode
docker
internal
app
applicant
certificate
deployer
domain
notify
pkg
core
logging
sdk3rd
utils
cert
common.go
converter.go
extractor.go
parser.go
transformer.go
file
http
map
slice
type
repository
rest
scheduler
statistics
workflow
migrations
ui
.dockerignore
.editorconfig
.gitignore
.goreleaser.yml
CHANGELOG.md
CONTRIBUTING.md
CONTRIBUTING_EN.md
Dockerfile
LICENSE.md
Makefile
README.md
README_EN.md
go.mod
go.sum
main.go
nixpacks.toml
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package certutil
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// 将 x509.Certificate 对象转换为 PEM 编码的字符串。
|
|
//
|
|
// 入参:
|
|
// - cert: x509.Certificate 对象。
|
|
//
|
|
// 出参:
|
|
// - certPEM: 证书 PEM 内容。
|
|
// - err: 错误。
|
|
func ConvertCertificateToPEM(cert *x509.Certificate) (certPEM string, err error) {
|
|
if cert == nil {
|
|
return "", errors.New("`cert` is nil")
|
|
}
|
|
|
|
block := &pem.Block{
|
|
Type: "CERTIFICATE",
|
|
Bytes: cert.Raw,
|
|
}
|
|
|
|
return string(pem.EncodeToMemory(block)), nil
|
|
}
|
|
|
|
// 将 ecdsa.PrivateKey 对象转换为 PEM 编码的字符串。
|
|
//
|
|
// 入参:
|
|
// - privkey: ecdsa.PrivateKey 对象。
|
|
//
|
|
// 出参:
|
|
// - privkeyPEM: 私钥 PEM 内容。
|
|
// - err: 错误。
|
|
func ConvertECPrivateKeyToPEM(privkey *ecdsa.PrivateKey) (privkeyPEM string, err error) {
|
|
if privkey == nil {
|
|
return "", errors.New("`privkey` is nil")
|
|
}
|
|
|
|
data, err := x509.MarshalECPrivateKey(privkey)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to marshal EC private key: %w", err)
|
|
}
|
|
|
|
block := &pem.Block{
|
|
Type: "EC PRIVATE KEY",
|
|
Bytes: data,
|
|
}
|
|
|
|
return string(pem.EncodeToMemory(block)), nil
|
|
}
|