package auth import ( "crypto/ecdsa" "crypto/x509" "encoding/pem" "fmt" ) // ParseECDSAPrivateKey 解析 PEM 编码的 ECDSA 私钥。 func ParseECDSAPrivateKey(pemData []byte) (*ecdsa.PrivateKey, error) { block, _ := pem.Decode(pemData) if block == nil || block.Type != "EC PRIVATE KEY" { return nil, fmt.Errorf("invalid EC private key PEM") } key, err := x509.ParseECPrivateKey(block.Bytes) if err != nil { return nil, fmt.Errorf("parse EC private key: %w", err) } return key, nil } // ParseECDSAPublicKey 解析 PEM 编码的 ECDSA 公钥。 func ParseECDSAPublicKey(pemData []byte) (*ecdsa.PublicKey, error) { block, _ := pem.Decode(pemData) if block == nil || block.Type != "PUBLIC KEY" { return nil, fmt.Errorf("invalid public key PEM") } key, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { return nil, fmt.Errorf("parse public key: %w", err) } pubKey, ok := key.(*ecdsa.PublicKey) if !ok { return nil, fmt.Errorf("key is not ECDSA") } return pubKey, nil }