mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-27 12:21:03 +00:00
39 lines
1 KiB
Go
39 lines
1 KiB
Go
package sshutil
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/code.google.com/p/go.crypto/ssh"
|
|
)
|
|
|
|
const (
|
|
RSA_BITS = 2048 // Default number of bits in an RSA key
|
|
RSA_BITS_MIN = 768 // Minimum number of bits in an RSA key
|
|
)
|
|
|
|
// helper function to generate an RSA Private Key.
|
|
func GeneratePrivateKey() (*rsa.PrivateKey, error) {
|
|
return rsa.GenerateKey(rand.Reader, RSA_BITS)
|
|
}
|
|
|
|
// helper function that marshalls an RSA Public Key to an SSH
|
|
// .authorized_keys format
|
|
func MarshalPublicKey(pubkey *rsa.PublicKey) []byte {
|
|
pk, err := ssh.NewPublicKey(pubkey)
|
|
if err != nil {
|
|
return []byte{}
|
|
}
|
|
|
|
return ssh.MarshalAuthorizedKey(pk)
|
|
}
|
|
|
|
// helper function that marshalls an RSA Private Key to
|
|
// a PEM encoded file.
|
|
func MarshalPrivateKey(privkey *rsa.PrivateKey) []byte {
|
|
privateKeyMarshaled := x509.MarshalPKCS1PrivateKey(privkey)
|
|
privateKeyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Headers: nil, Bytes: privateKeyMarshaled})
|
|
return privateKeyPEM
|
|
}
|