1 Star 0 Fork 0

powerpaas / machine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
keys.go 2.66 KB
一键复制 编辑 原始数据 按行查看 历史
Ivan Markin 提交于 2018-02-10 20:51 . Do chmod on ssh key on OpenBSD too
package ssh
import (
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"io"
"os"
"runtime"
gossh "golang.org/x/crypto/ssh"
)
var (
ErrKeyGeneration = errors.New("Unable to generate key")
ErrValidation = errors.New("Unable to validate key")
ErrPublicKey = errors.New("Unable to convert public key")
ErrUnableToWriteFile = errors.New("Unable to write file")
)
type KeyPair struct {
PrivateKey []byte
PublicKey []byte
}
// NewKeyPair generates a new SSH keypair
// This will return a private & public key encoded as DER.
func NewKeyPair() (keyPair *KeyPair, err error) {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, ErrKeyGeneration
}
if err := priv.Validate(); err != nil {
return nil, ErrValidation
}
privDer := x509.MarshalPKCS1PrivateKey(priv)
pubSSH, err := gossh.NewPublicKey(&priv.PublicKey)
if err != nil {
return nil, ErrPublicKey
}
return &KeyPair{
PrivateKey: privDer,
PublicKey: gossh.MarshalAuthorizedKey(pubSSH),
}, nil
}
// WriteToFile writes keypair to files
func (kp *KeyPair) WriteToFile(privateKeyPath string, publicKeyPath string) error {
files := []struct {
File string
Type string
Value []byte
}{
{
File: privateKeyPath,
Value: pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Headers: nil, Bytes: kp.PrivateKey}),
},
{
File: publicKeyPath,
Value: kp.PublicKey,
},
}
for _, v := range files {
f, err := os.Create(v.File)
if err != nil {
return ErrUnableToWriteFile
}
if _, err := f.Write(v.Value); err != nil {
return ErrUnableToWriteFile
}
// windows does not support chmod
switch runtime.GOOS {
case "darwin", "freebsd", "linux", "openbsd":
if err := f.Chmod(0600); err != nil {
return err
}
}
}
return nil
}
// Fingerprint calculates the fingerprint of the public key
func (kp *KeyPair) Fingerprint() string {
b, _ := base64.StdEncoding.DecodeString(string(kp.PublicKey))
h := md5.New()
io.WriteString(h, string(b))
return fmt.Sprintf("%x", h.Sum(nil))
}
// GenerateSSHKey generates SSH keypair based on path of the private key
// The public key would be generated to the same path with ".pub" added
func GenerateSSHKey(path string) error {
if _, err := os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("Desired directory for SSH keys does not exist: %s", err)
}
kp, err := NewKeyPair()
if err != nil {
return fmt.Errorf("Error generating key pair: %s", err)
}
if err := kp.WriteToFile(path, fmt.Sprintf("%s.pub", path)); err != nil {
return fmt.Errorf("Error writing keys to file(s): %s", err)
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/powerpaas/machine.git
git@gitee.com:powerpaas/machine.git
powerpaas
machine
machine
v0.15.0-rancher20

搜索帮助

344bd9b3 5694891 D2dac590 5694891