package main import ( "fmt" "golang.org/x/crypto/ssh" ) func main() { pubKeyBytes := []byte("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC4yC4...") pubKey, _, _, _, err := ssh.ParseAuthorizedKey(pubKeyBytes) if err != nil { panic(err) } rsaPubKey, ok := pubKey.(*ssh.RSAPublicKey) if !ok { panic("not an RSA public key") } fmt.Println("RSA key length:", rsaPubKey.N.BitLen()) }
package main import ( "golang.org/x/crypto/ssh" "io/ioutil" ) func main() { pubKeyBytes, err := ioutil.ReadFile("authorized_keys") if err != nil { panic(err) } pubKey, _, _, _, err := ssh.ParseAuthorizedKey(pubKeyBytes) if err != nil { panic(err) } if pubKey.Type() != ssh.KeyAlgoRSA { panic("not an RSA public key") } rsaPubKey := pubKey.(*ssh.RSAPublicKey) username := "john" authorized := false // Check if the RSA public key is authorized for the user "john" ssh.AuthorizedKeyCallback(func(_ ssh.PublicKey, user string, _ ssh.ConnMetadata) error { if user == username && rsaPubKey.Equal(_.(*ssh.RSAPublicKey)) { authorized = true } return nil }) // If the public key was authorized, do something... if authorized { // ... } }This example reads the authorized SSH public keys from a file, parses them, and checks if a specific key is authorized for a given user. It uses the ssh.AuthorizedKeyCallback function, which is part of the golang.org.x/crypto/ssh package.