本文整理汇总了Golang中golang.org/x/crypto/ssh.MarshalAuthorizedKey函数的典型用法代码示例。如果您正苦于以下问题:Golang MarshalAuthorizedKey函数的具体用法?Golang MarshalAuthorizedKey怎么用?Golang MarshalAuthorizedKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MarshalAuthorizedKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAgentForward
func TestAgentForward(t *testing.T) {
server := newServer(t)
defer server.Shutdown()
conn := server.Dial(clientConfig())
defer conn.Close()
keyring := agent.NewKeyring()
keyring.Add(testPrivateKeys["dsa"], nil, "")
pub := testPublicKeys["dsa"]
sess, err := conn.NewSession()
if err != nil {
t.Fatalf("NewSession: %v", err)
}
if err := agent.RequestAgentForwarding(sess); err != nil {
t.Fatalf("RequestAgentForwarding: %v", err)
}
if err := agent.ForwardToAgent(conn, keyring); err != nil {
t.Fatalf("SetupForwardKeyring: %v", err)
}
out, err := sess.CombinedOutput("ssh-add -L")
if err != nil {
t.Fatalf("running ssh-add: %v, out %s", err, out)
}
key, _, _, _, err := ssh.ParseAuthorizedKey(out)
if err != nil {
t.Fatalf("ParseAuthorizedKey(%q): %v", out, err)
}
if !bytes.Equal(key.Marshal(), pub.Marshal()) {
t.Fatalf("got key %s, want %s", ssh.MarshalAuthorizedKey(key), ssh.MarshalAuthorizedKey(pub))
}
}
示例2: theseTwoPublicKeysAreEqual
func theseTwoPublicKeysAreEqual(one, other ssh.PublicKey) bool {
oneKeyMshld := ssh.MarshalAuthorizedKey(one)
otherKeyMshld := ssh.MarshalAuthorizedKey(other)
if len(oneKeyMshld) != len(otherKeyMshld) {
return false
}
for i, elm := range oneKeyMshld {
if elm != otherKeyMshld[i] {
return false
}
}
return true
}
示例3: GenerateSSHKeyPair
// GenerateSSHKeyPair generates new key-pair for use with SSH.
func GenerateSSHKeyPair() (*SSHKeyPair, error) {
priv, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return nil, err
}
var privBuf bytes.Buffer
privPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(priv),
}
if err := pem.Encode(&privBuf, privPEM); err != nil {
return nil, err
}
pub, err := ssh.NewPublicKey(&priv.PublicKey)
if err != nil {
return nil, err
}
key := &SSHKeyPair{
Private: bytes.TrimSpace(privBuf.Bytes()),
Public: bytes.TrimSpace(ssh.MarshalAuthorizedKey(pub)),
}
sum := sha1.Sum(key.Private)
key.Name = "koding-ssh-keypair-" + hex.EncodeToString(sum[:])
return key, nil
}
示例4: ParseKey
// ParseKey reads the given RSA private key and create a public one for it.
func ParseKey(pem string) (*Key, error) {
p, err := ioutil.ReadFile(pem)
if err != nil {
return nil, err
}
key, err := ssh.ParseRawPrivateKey(p)
if err != nil {
return nil, err
}
rsaKey, ok := key.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("%q is not a RSA key", pem)
}
pub, err := ssh.NewPublicKey(&rsaKey.PublicKey)
if err != nil {
return nil, err
}
// Compute key fingerprint.
var buf bytes.Buffer
for _, b := range md5.Sum(pub.Marshal()) {
fmt.Fprintf(&buf, "%0.2x:", b)
}
return &Key{
Label: strings.TrimSuffix(filepath.Base(pem), ".pem"), // trim .pem file extension
Key: string(bytes.TrimRight(ssh.MarshalAuthorizedKey(pub), "\n")), // trim newline
Fingerprint: string(bytes.TrimRight(buf.Bytes(), ":")), // trim dangling colon
Note: "{}",
Tags: make(Tags),
}, nil
}
示例5: HideKey
func (g GitConfig) HideKey() GitConfig {
if g.Key == "" {
return g
}
key, err := ssh.ParseRawPrivateKey([]byte(g.Key))
if err != nil {
g.Key = secretReplacement
return g
}
privKey, ok := key.(*rsa.PrivateKey)
if !ok {
g.Key = secretReplacement
return g
}
pubKey, err := ssh.NewPublicKey(&privKey.PublicKey)
if err != nil {
g.Key = secretReplacement
return g
}
g.Key = string(ssh.MarshalAuthorizedKey(pubKey))
return g
}
示例6: handleAuth
func handleAuth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
if *authHook == "" {
// allow all
return &ssh.Permissions{
Extensions: map[string]string{
"user": conn.User(),
},
}, nil
}
keydata := string(bytes.TrimSpace(ssh.MarshalAuthorizedKey(key)))
cmd, err := handlerCmd(*authHook, conn.User(), keydata)
if err != nil {
return nil, err
}
var output bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = &output
status, err := exitStatus(cmd.Run())
if err != nil {
return nil, err
}
if status.Status == 0 {
return &ssh.Permissions{
Extensions: map[string]string{
"environ": strings.Trim(output.String(), "\n"),
"user": conn.User(),
},
}, nil
}
debug("authentication hook status:", status.Status)
return nil, fmt.Errorf("authentication failed")
}
示例7: generateSshKeyPair
func generateSshKeyPair() Keypair {
priv, err := rsa.GenerateKey(rand.Reader, 2014)
if err != nil {
util.Fatalf("Error generating key", err)
}
// Get der format. priv_der []byte
priv_der := x509.MarshalPKCS1PrivateKey(priv)
// pem.Block
// blk pem.Block
priv_blk := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: priv_der,
}
// Resultant private key in PEM format.
// priv_pem string
priv_pem := string(pem.EncodeToMemory(&priv_blk))
// Public Key generation
sshPublicKey, err := ssh.NewPublicKey(&priv.PublicKey)
pubBytes := ssh.MarshalAuthorizedKey(sshPublicKey)
return Keypair{
pub: []byte(pubBytes),
priv: []byte(priv_pem),
}
}
示例8: EncodeSSHKey
func EncodeSSHKey(public *rsa.PublicKey) ([]byte, error) {
publicKey, err := ssh.NewPublicKey(public)
if err != nil {
return nil, err
}
return ssh.MarshalAuthorizedKey(publicKey), nil
}
示例9: CmdAdd
func CmdAdd(name, keyPath, svcName string, id IDeployKeys, is services.IServices) error {
if strings.ContainsAny(name, config.InvalidChars) {
return fmt.Errorf("Invalid SSH key name. Names must not contain the following characters: %s", config.InvalidChars)
}
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
return fmt.Errorf("A file does not exist at path '%s'", keyPath)
}
service, err := is.RetrieveByLabel(svcName)
if err != nil {
return err
}
if service == nil {
return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
}
if service.Type != "code" {
return fmt.Errorf("You can only add deploy keys to code services, not %s services", service.Type)
}
key, err := ioutil.ReadFile(keyPath)
if err != nil {
return err
}
k, err := id.ParsePublicKey(key)
if err != nil {
return err
}
key = ssh.MarshalAuthorizedKey(k)
return id.Add(name, "ssh", string(key), service.ID)
}
示例10: sshkey
func sshkey(bits int) (string, string, string, error) {
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return "", "", "", err
}
private := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
},
)
pub := key.Public()
pubkey, err := ssh.NewPublicKey(pub)
if err != nil {
return "", "", "", err
}
public := ssh.MarshalAuthorizedKey(pubkey)
var fp []string
f := []byte(fmt.Sprintf("%x", md5.Sum(pubkey.Marshal())))
for i := 0; i < len(f); i += 2 {
fp = append(fp, string(f[i:i+2]))
}
fingerprint := strings.Join(fp, ":")
return string(private), string(public), string(fingerprint), nil
}
示例11: CreatePrivateKey
func CreatePrivateKey(d *schema.ResourceData, meta interface{}) error {
keyAlgoName := d.Get("algorithm").(string)
var keyFunc keyAlgo
var ok bool
if keyFunc, ok = keyAlgos[keyAlgoName]; !ok {
return fmt.Errorf("invalid key_algorithm %#v", keyAlgoName)
}
key, err := keyFunc(d)
if err != nil {
return err
}
var keyPemBlock *pem.Block
switch k := key.(type) {
case *rsa.PrivateKey:
keyPemBlock = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(k),
}
case *ecdsa.PrivateKey:
keyBytes, err := x509.MarshalECPrivateKey(k)
if err != nil {
return fmt.Errorf("error encoding key to PEM: %s", err)
}
keyPemBlock = &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: keyBytes,
}
default:
return fmt.Errorf("unsupported private key type")
}
keyPem := string(pem.EncodeToMemory(keyPemBlock))
pubKey := publicKey(key)
pubKeyBytes, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return fmt.Errorf("failed to marshal public key: %s", err)
}
pubKeyPemBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: pubKeyBytes,
}
d.SetId(hashForState(string((pubKeyBytes))))
d.Set("private_key_pem", keyPem)
d.Set("public_key_pem", string(pem.EncodeToMemory(pubKeyPemBlock)))
sshPubKey, err := ssh.NewPublicKey(pubKey)
if err == nil {
// Not all EC types can be SSH keys, so we'll produce this only
// if an appropriate type was selected.
sshPubKeyBytes := ssh.MarshalAuthorizedKey(sshPubKey)
d.Set("public_key_openssh", string(sshPubKeyBytes))
} else {
d.Set("public_key_openssh", "")
}
return nil
}
示例12: getSshKey
func getSshKey(d *schema.ResourceData, path string) (privatekey string, publickey string, err error) {
pemBytes, err := ioutil.ReadFile(path)
if err != nil {
return "", "", err
}
block, _ := pem.Decode(pemBytes)
if block == nil {
return "", "", errors.New("File " + path + " contains nothing")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return "", "", err
}
priv_blk := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: x509.MarshalPKCS1PrivateKey(priv),
}
pub, err := ssh.NewPublicKey(&priv.PublicKey)
if err != nil {
return "", "", err
}
publickey = string(ssh.MarshalAuthorizedKey(pub))
privatekey = string(pem.EncodeToMemory(&priv_blk))
return privatekey, publickey, nil
}
示例13: RSAPubKeyToDisk
// Serialize an RSA public key to disk format, specifically to the
// format used by SSH. Should return nil if the conversion fails.
func RSAPubKeyToDisk(rsaPubKey *rsa.PublicKey) (out []byte, err error) {
pubKey, err := ssh.NewPublicKey(rsaPubKey)
if err == nil {
out = ssh.MarshalAuthorizedKey(pubKey)
}
return out, nil
}
示例14: NewRandomKeyPair
func NewRandomKeyPair() (*KeyPair, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: privateKeyDer,
}
privateKeyPem := string(pem.EncodeToMemory(&privateKeyBlock))
publicKey := privateKey.PublicKey
pub, err := ssh.NewPublicKey(&publicKey)
if err != nil {
return nil, err
}
pubBytes := ssh.MarshalAuthorizedKey(pub)
return &KeyPair{
PublicKey: string(pubBytes),
PrivateKey: privateKeyPem,
}, nil
}
示例15: makeSSHKeyPair
// MakeSSHKeyPair make a pair of public and private keys for SSH access.
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
// Private Key generated is PEM encoded
// StackOverflow: Greg http://stackoverflow.com/users/328645/greg in
// http://stackoverflow.com/questions/21151714/go-generate-an-ssh-public-key
// No licence added
func makeSSHKeyPair(bits int, pubKeyPath, privateKeyPath string) error {
if bits < 1024 {
return errors.New("Reject using too few bits for key")
}
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
// generate and write private key as PEM
privateKeyFile, err := os.OpenFile(privateKeyPath, os.O_WRONLY|os.O_CREATE, 0600)
defer privateKeyFile.Close()
if err != nil {
return err
}
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
return err
}
// generate and write public key
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return err
}
return ioutil.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0600)
}