SSH Public Key
Match SSH public keys in OpenSSH `authorized_keys` format, including the optional comment field.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("ssh-(?:rsa|dss|ed25519|ecdsa-sha2-nistp(?:256|384|521))\\s+[A-Za-z0-9+\\/=]+(?:\\s+\\S+)?", "g");
const input = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBVxr alice@laptop";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"ssh-(?:rsa|dss|ed25519|ecdsa-sha2-nistp(?:256|384|521))\s+[A-Za-z0-9+\/=]+(?:\s+\S+)?")
input_text = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBVxr alice@laptop"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
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.
Pattern
ssh-(?:rsa|dss|ed25519|ecdsa-sha2-nistp(?:256|384|521))\s+[A-Za-z0-9+\/=]+(?:\s+\S+)? (flags: g)Raw source: ssh-(?:rsa|dss|ed25519|ecdsa-sha2-nistp(?:256|384|521))\s+[A-Za-z0-9+\/=]+(?:\s+\S+)?
How it works
Examples
Input
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBVxr alice@laptopMatches
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBVxr alice@laptop
Input
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABMatches
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB
Input
PRIVATE KEY HERENo match
—Common use cases
- •Validating uploaded SSH keys in admin UIs
- •Auditing authorized_keys files for stray algorithms
- •Secret-scanning for committed public keys (info leakage)
- •Provisioning automation that ingests user-supplied keys
Related patterns
Generic API Key
SecurityMatch generic long alphanumeric tokens (32+ chars) typical of API keys and access tokens.
PEM Private Key Block
SecurityMatch PEM-encoded private key blocks across the common variants (RSA, EC, DSA, OpenSSH, encrypted, PGP).
Stripe API Key
SecurityMatch Stripe API keys: secret (sk_), publishable (pk_), or restricted (rk_), in test or live mode.
AWS Access Key ID
SecurityMatch AWS access key IDs (both long-term AKIA and temporary ASIA prefixes).