Add key generation.

This commit is contained in:
Brendan Burns
2015-05-28 11:45:08 -07:00
committed by CJ Cullen
parent 30a89968a4
commit 5115fd5703
13 changed files with 162 additions and 5 deletions

View File

@@ -18,10 +18,14 @@ package util
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"math/rand"
mathrand "math/rand"
"net"
"os"
@@ -260,7 +264,7 @@ func (l SSHTunnelList) Dial(network, addr string) (net.Conn, error) {
if len(l) == 0 {
return nil, fmt.Errorf("Empty tunnel list.")
}
return l[rand.Int()%len(l)].Tunnel.Dial(network, addr)
return l[mathrand.Int()%len(l)].Tunnel.Dial(network, addr)
}
func (l SSHTunnelList) Has(addr string) bool {
@@ -271,3 +275,38 @@ func (l SSHTunnelList) Has(addr string) bool {
}
return false
}
func EncodePrivateKey(private *rsa.PrivateKey) []byte {
return pem.EncodeToMemory(&pem.Block{
Bytes: x509.MarshalPKCS1PrivateKey(private),
Type: "RSA PRIVATE KEY",
})
}
func EncodePublicKey(public *rsa.PublicKey) ([]byte, error) {
publicBytes, err := x509.MarshalPKIXPublicKey(public)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(&pem.Block{
Bytes: publicBytes,
Type: "PUBLIC KEY",
}), nil
}
func EncodeSSHKey(public *rsa.PublicKey) ([]byte, error) {
publicKey, err := ssh.NewPublicKey(public)
if err != nil {
return nil, err
}
return ssh.MarshalAuthorizedKey(publicKey), nil
}
func GenerateKey(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error) {
private, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil, err
}
return private, &private.PublicKey, nil
}

View File

@@ -515,4 +515,16 @@ func ShortenString(str string, n int) string {
} else {
return str[:n]
}
func FileExists(filename string) (bool, error) {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}
return true, nil
}