Compare commits

..

2 Commits

Author SHA1 Message Date
兔子 e722b958a4 add openssh private key method 2 months ago
兔子 f3f50b4f6c update 8 months ago

@ -3,11 +3,14 @@ package starcrypto
import ( import (
"crypto" "crypto"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors" "errors"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
"reflect"
) )
func EncodePrivateKey(private crypto.PrivateKey, secret string) ([]byte, error) { func EncodePrivateKey(private crypto.PrivateKey, secret string) ([]byte, error) {
@ -17,7 +20,22 @@ func EncodePrivateKey(private crypto.PrivateKey, secret string) ([]byte, error)
case *ecdsa.PrivateKey: case *ecdsa.PrivateKey:
return EncodeEcdsaPrivateKey(private.(*ecdsa.PrivateKey), secret) return EncodeEcdsaPrivateKey(private.(*ecdsa.PrivateKey), secret)
default: default:
return nil, errors.New("private key type error") b, err := x509.MarshalPKCS8PrivateKey(private)
if err != nil {
return nil, err
}
if secret == "" {
return pem.EncodeToMemory(&pem.Block{
Bytes: b,
Type: "PRIVATE KEY",
}), err
}
chiper := x509.PEMCipherAES256
blk, err := x509.EncryptPEMBlock(rand.Reader, "PRIVATE KEY", b, []byte(secret), chiper)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(blk), err
} }
} }
@ -28,7 +46,14 @@ func EncodePublicKey(public crypto.PublicKey) ([]byte, error) {
case *ecdsa.PublicKey: case *ecdsa.PublicKey:
return EncodeEcdsaPublicKey(public.(*ecdsa.PublicKey)) return EncodeEcdsaPublicKey(public.(*ecdsa.PublicKey))
default: default:
return nil, errors.New("public key type error") publicBytes, err := x509.MarshalPKIXPublicKey(public)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(&pem.Block{
Bytes: publicBytes,
Type: "PUBLIC KEY",
}), nil
} }
} }
@ -42,11 +67,63 @@ func DecodePrivateKey(private []byte, password string) (crypto.PrivateKey, error
return DecodeRsaPrivateKey(private, password) return DecodeRsaPrivateKey(private, password)
case "EC PRIVATE KEY": case "EC PRIVATE KEY":
return DecodeEcdsaPrivateKey(private, password) return DecodeEcdsaPrivateKey(private, password)
case "PRIVATE KEY":
var prikey crypto.PrivateKey
var err error
var bytes []byte
blk, _ := pem.Decode(private)
if blk == nil {
return nil, errors.New("private key error")
}
if password != "" {
tmp, err := x509.DecryptPEMBlock(blk, []byte(password))
if err != nil {
return nil, err
}
bytes = tmp
} else {
bytes = blk.Bytes
}
prikey, err = x509.ParsePKCS8PrivateKey(bytes)
if err != nil {
return nil, err
}
return prikey, err
case "OPENSSH PRIVATE KEY":
var err error
var priv crypto.PrivateKey
if password == "" {
priv, err = ssh.ParseRawPrivateKey(private)
if err != nil {
return nil, err
}
} else {
priv, err = ssh.ParseRawPrivateKeyWithPassphrase(private, []byte(password))
if err != nil {
return nil, err
}
}
return priv, nil
default: default:
return nil, errors.New("private key type error") return nil, errors.New("private key type error")
} }
} }
func EncodeOpenSSHPrivateKey(private crypto.PrivateKey, secret string) ([]byte, error) {
var key interface{} = private
var block *pem.Block
var err error
if reflect.TypeOf(key) == reflect.TypeOf(&ed25519.PrivateKey{}) {
key = *(key.(*ed25519.PrivateKey))
}
if secret == "" {
block, err = ssh.MarshalPrivateKey(key, "")
} else {
block, err = ssh.MarshalPrivateKeyWithPassphrase(key, "", []byte(secret))
}
return pem.EncodeToMemory(block), err
}
func DecodePublicKey(pubStr []byte) (crypto.PublicKey, error) { func DecodePublicKey(pubStr []byte) (crypto.PublicKey, error) {
blk, _ := pem.Decode(pubStr) blk, _ := pem.Decode(pubStr)
if blk == nil { if blk == nil {
@ -66,3 +143,7 @@ func EncodeSSHPublicKey(public crypto.PublicKey) ([]byte, error) {
} }
return ssh.MarshalAuthorizedKey(publicKey), nil return ssh.MarshalAuthorizedKey(publicKey), nil
} }
func DecodeSSHPublicKey(pubStr []byte) (crypto.PublicKey, error) {
return ssh.ParsePublicKey(pubStr)
}

Loading…
Cancel
Save