1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
14
"code.google.com/p/go.crypto/ssh"
17
// KeyBits is used to determine the number of bits to use for the RSA keys
18
// created using the GenerateKey function.
21
// GenerateKey makes a 2048 bit RSA no-passphrase SSH capable key. The bit
22
// size is actually controlled by the KeyBits var. The private key returned is
23
// encoded to ASCII using the PKCS1 encoding. The public key is suitable to
24
// be added into an authorized_keys file, and has the comment passed in as the
25
// comment part of the key.
26
func GenerateKey(comment string) (private, public string, err error) {
27
key, err := rsa.GenerateKey(rand.Reader, KeyBits)
32
identity := pem.EncodeToMemory(
34
Type: "RSA PRIVATE KEY",
35
Bytes: x509.MarshalPKCS1PrivateKey(key),
38
signer, err := ssh.ParsePrivateKey(identity)
40
return "", "", fmt.Errorf("failed to load key: %v", err)
43
auth_key := string(ssh.MarshalAuthorizedKey(signer.PublicKey()))
44
// Strip off the trailing new line so we can add a comment.
45
auth_key = strings.TrimSpace(auth_key)
46
public = fmt.Sprintf("%s %s\n", auth_key, comment)
48
return string(identity), public, nil