Go (RE2)

SSH Public Key in GO

Match SSH public keys in OpenSSH `authorized_keys` format, including the optional comment field.

Try it in the GO tester →

Pattern

regexGO
ssh-(?:rsa|dss|ed25519|ecdsa-sha2-nistp(?:256|384|521))\s+[A-Za-z0-9+\/=]+(?:\s+\S+)?   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`ssh-(?:rsa|dss|ed25519|ecdsa-sha2-nistp(?:256|384|521))\s+[A-Za-z0-9+\/=]+(?:\s+\S+)?`)
	input := `ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBVxr alice@laptop`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

ssh-(?:rsa|dss|ed25519|ecdsa-sha2-nistp(?:256|384|521)) matches the key-type prefix for the four common algorithms. \s+ requires whitespace before the base64 body. [A-Za-z0-9+\/=]+ matches the base64-encoded key material. (?:\s+\S+)? optionally matches a trailing comment (typically `user@host`).

Examples

Input

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBVxr alice@laptop

Matches

  • ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBVxr alice@laptop

Input

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB

Matches

  • ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB

Input

PRIVATE KEY HERE

No match

Same pattern, other engines

← Back to SSH Public Key overview (all engines)