本文整理匯總了Golang中code/google/com/p/go/crypto/ssh.ParseAuthorizedKey函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseAuthorizedKey函數的具體用法?Golang ParseAuthorizedKey怎麽用?Golang ParseAuthorizedKey使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ParseAuthorizedKey函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: newTestServer
func newTestServer() (Server, error) {
// Create a new backend object.
b := redis.New("test", "tcp", "127.0.0.1:6379")
// Create a new crypter.
c, err := crypto.NewRandomCrypter()
if err != nil {
return nil, err
}
private, _ := ssh.ParsePrivateKey(ServerTestPrivateKey)
if err != nil {
return nil, err
}
s := NewServer(b, c, private)
for _, publicKey := range ServerTestPublicKeys {
publicKeyParsed, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
if err != nil {
return s, err
}
s.AddReadKey(publicKeyParsed)
s.AddWriteKey(publicKeyParsed)
}
return s, nil
}
示例2: ReadUserAuthKeys
func ReadUserAuthKeys(u string) (publicKeys []ssh.PublicKey, err error) {
uacc, err := user.Lookup(u)
if err != nil {
return
}
b, err := ioutil.ReadFile(path.Join(uacc.HomeDir, ".ssh/authorized_keys"))
if err != nil {
return
}
buf := bytes.NewBuffer(b)
for {
line, err := buf.ReadBytes('\n')
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if strings.HasPrefix(string(line), "#") {
continue
}
publicKey, _, _, _, err := ssh.ParseAuthorizedKey(line)
if err != nil {
return nil, err
}
publicKeys = append(publicKeys, publicKey)
}
return
}
示例3: CheckHostKey
func CheckHostKey(HostKey string) (checkHostKey func(string, net.Addr, ssh.PublicKey) error) {
var err error
var public ssh.PublicKey
var publices []ssh.PublicKey
rest := []byte(HostKey)
for {
public, _, _, rest, err = ssh.ParseAuthorizedKey(rest)
if err != nil {
err = nil
break
}
publices = append(publices, public)
}
checkHostKey = func(hostname string, remote net.Addr, key ssh.PublicKey) (err error) {
hostkey := key.Marshal()
log.Debug("remote hostkey: %s, type: %s", hostname, key.Type())
for _, public := range publices {
if key.Type() == public.Type() && bytes.Compare(hostkey, public.Marshal()) == 0 {
log.Info("host key match: %s", hostname)
return nil
}
}
log.Info("host key not match: %s", hostname)
return ErrHostKey
}
return
}
示例4: 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))
}
}
示例5: TestSSHFingerprint
func TestSSHFingerprint(t *testing.T) {
Convey("Test generating SSH fingerprint", t, func() {
pubkey, _, _, _, err := ssh.ParseAuthorizedKey([]byte("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXg9Vmhy9YSB8BcN3yHgQjdX9lN3j2KRpv7kVDXSiIana2WbKP7IiTS0uJcJWUM3vlHjdL9KOO0jCWWzVFIcmLhiVVG+Fy2tothBp/NhjR8WWG/6Jg/6tXvVkLG6bDgfbDaLWdE5xzjL0YG8TrIluqnu0J5GHKrQcXF650PlqkGo+whpXrS8wOG+eUmsHX9L1w/Z3TkQlMjQNJEoRbqqSrp7yGj4JqzbtLpsglPRlobD7LHp+5ZDxzpk9i+6hoMxp2muDFxnEtZyED6IMQlNNEGkc3sdmGPOo26oW2+ePkBcjpOpdVif/Iya/jDLuLFHAOol6G34Tr4IdTgaL0qCCr TEST KEY"))
panic_the_err(err)
fingerprint := SSHFingerprint(pubkey)
So(fingerprint, ShouldEqual, "c0:61:84:fc:e8:c9:52:dc:cd:a9:8e:82:a2:70:0a:30")
})
}
示例6: TestEncode
func TestEncode(t *testing.T) {
Convey("Test Encode A String", t, func() {
pubkey, _, _, _, err := ssh.ParseAuthorizedKey([]byte("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXg9Vmhy9YSB8BcN3yHgQjdX9lN3j2KRpv7kVDXSiIana2WbKP7IiTS0uJcJWUM3vlHjdL9KOO0jCWWzVFIcmLhiVVG+Fy2tothBp/NhjR8WWG/6Jg/6tXvVkLG6bDgfbDaLWdE5xzjL0YG8TrIluqnu0J5GHKrQcXF650PlqkGo+whpXrS8wOG+eUmsHX9L1w/Z3TkQlMjQNJEoRbqqSrp7yGj4JqzbtLpsglPRlobD7LHp+5ZDxzpk9i+6hoMxp2muDFxnEtZyED6IMQlNNEGkc3sdmGPOo26oW2+ePkBcjpOpdVif/Iya/jDLuLFHAOol6G34Tr4IdTgaL0qCCr TEST KEY"))
panic_the_err(err)
plaintext := "some plaintext"
ciphertext, err := CredulousEncode(plaintext, pubkey)
So(err, ShouldEqual, nil)
So(len(ciphertext), ShouldEqual, 556)
})
}
示例7: ParseAuthorisedKey
// ParseAuthorisedKey parses a non-comment line from an
// authorized_keys file and returns the constituent parts.
// Based on description in "man sshd".
func ParseAuthorisedKey(line string) (*AuthorisedKey, error) {
key, comment, _, _, ok := ssh.ParseAuthorizedKey([]byte(line))
if !ok {
return nil, fmt.Errorf("invalid authorized_key %q", line)
}
keyBytes := ssh.MarshalPublicKey(key)
return &AuthorisedKey{
Key: keyBytes,
Comment: comment,
}, nil
}
示例8: ParseAuthorisedKey
// ParseAuthorisedKey parses a non-comment line from an
// authorized_keys file and returns the constituent parts.
// Based on description in "man sshd".
func ParseAuthorisedKey(line string) (*AuthorisedKey, error) {
key, comment, _, _, err := ssh.ParseAuthorizedKey([]byte(line))
if err != nil {
return nil, fmt.Errorf("invalid authorized_key %q", line)
}
return &AuthorisedKey{
Type: key.Type(),
Key: key.Marshal(),
Comment: comment,
}, nil
}
示例9: readSSHPubkeyFile
func readSSHPubkeyFile(filename string) (pubkey ssh.PublicKey, err error) {
pubkeyString, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
pubkey, _, _, _, err = ssh.ParseAuthorizedKey([]byte(pubkeyString))
if err != nil {
return nil, err
}
return pubkey, nil
}
示例10: newKey
func newKey(name, user, raw string) (*Key, error) {
key, comment, _, _, ok := ssh.ParseAuthorizedKey([]byte(raw))
if !ok {
return nil, ErrInvalidKey
}
body := ssh.MarshalAuthorizedKey(key)
k := Key{
Name: name,
Body: string(body),
Comment: comment,
UserName: user,
}
return &k, nil
}
示例11: newKey
func newKey(name, user, raw string) (*Key, error) {
key, comment, _, _, err := ssh.ParseAuthorizedKey([]byte(raw))
if err != nil {
return nil, ErrInvalidKey
}
body := ssh.MarshalAuthorizedKey(key.(ssh.PublicKey))
k := Key{
Name: name,
Body: string(body),
Comment: comment,
UserName: user,
CreatedAt: time.Now(),
}
return &k, nil
}
示例12: testAuthorizedKeys
func testAuthorizedKeys(t *testing.T, authKeys []byte, expected []authResult) {
rest := authKeys
var values []authResult
for len(rest) > 0 {
var r authResult
r.pubKey, r.comments, r.options, rest, r.ok = ssh.ParseAuthorizedKey(rest)
r.rest = string(rest)
values = append(values, r)
}
if !reflect.DeepEqual(values, expected) {
t.Errorf("got %q, expected %q", values, expected)
}
}
示例13: TestHostKeyChecker
// TestHostKeyChecker tests to check existing key
func TestHostKeyChecker(t *testing.T) {
keyFile := NewHostKeyFile(hostFile)
checker := NewHostKeyChecker(keyFile)
addr, key, _ := parseKnownHostsLine([]byte(hostLine))
tcpAddr, _ := net.ResolveTCPAddr("tcp", addr)
if err := checker.Check("localhost", tcpAddr, key); err != nil {
t.Fatalf("checker should succeed for %v: %v", tcpAddr.String(), err)
}
wrongKey, _, _, _, _ := gossh.ParseAuthorizedKey([]byte(wrongAuthorizedKey))
if err := checker.Check("localhost", tcpAddr, wrongKey); err != ErrUnmatchKey {
t.Fatalf("checker should fail with %v", ErrUnmatchKey)
}
}
示例14: serverFetchPublicKeys
func serverFetchPublicKeys(url string) ([]ssh.PublicKey, error) {
// Fetch the public keys.
response, err := serverClient.Get(url)
if err != nil {
return nil, err
}
// Read out the entire body.
jsonResponse, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
// Build a raw keys object that reflects the expected structure of the JSON.
var rawKeys []struct {
Key string
}
// Try to parse the body of the response as JSON.
if err := json.Unmarshal(jsonResponse, &rawKeys); err != nil {
return nil, err
}
// Build a new authorizer and iterate through the raw keys, parsing them
// and then adding them.
publicKeys := make([]ssh.PublicKey, 0, len(rawKeys))
for _, rawKey := range rawKeys {
// We're only interested in the key itself and whether or not there was an error.
publicKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(rawKey.Key))
if err != nil {
return publicKeys, err
}
// Add the key to the list.
publicKeys = publicKeys[:len(publicKeys)+1]
publicKeys[len(publicKeys)-1] = publicKey
}
return publicKeys, nil
}
示例15: sshReadPubKey
// Read SSH public key bytes from path
func sshReadPubKey(path string) (ssh.PublicKey, string, error) {
f, err := os.Open(filepath.FromSlash(path))
if err != nil {
return nil, "", err
}
keyBytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, "", err
}
if bytes.Contains(keyBytes, []byte("PRIVATE")) {
return nil, "", errPrivKey(path)
}
key, comment, _, _, ok := ssh.ParseAuthorizedKey(keyBytes)
if !ok {
return nil, "", errNotKey
}
return key, comment, nil
}