Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 678955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:12:24+00:00 2026-05-14T01:12:24+00:00

What regular expression can I use (if any) to validate that a given string

  • 0

What regular expression can I use (if any) to validate that a given string is a legal ssh rsa public key?

I only need to validate the actual key – I don’t care about the key type the precedes it or the username comment after it.

Ideally, someone will also provide the python code to run the regex validation.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:12:24+00:00Added an answer on May 14, 2026 at 1:12 am

    A “good enough” check is to see if the key starts with the correct header.

    The data portion of the keyfile should decode from base64, or it will fail with a base64.binascii.Error

    Unpack the first 4 bytes (an int), which should be 7. This is the
    length of the following string (I guess this could be different, but you’re only concerned with ssh-rsa).

    openssh_pubkey = open('keyfile').read()
    type, key_string, comment = openssh_pubkey.split()
    data = base64.decodestring(key_string)
    int_len = 4
    str_len = struct.unpack('>I', data[:int_len])[0] # this should return 7
    data[int_len:int_len+str_len] == type
    

    Alternatively, you could forgo the binary checks, and look for AAAAB3NzaC1yc2EA at the start of an ssh-rsa key, bit I would still verify it’s valid base64.

    [edit] Clarification:
    Via the specification, the first part if the key is a length prefixed string. The length is packed as a big-endian unsigned int (‘>I’ for a python struct). It’s a 7 here, because the following string, ‘ssh-rsa’, is 7 bytes long. data[4:11] is the next 7 bytes (per the length prefix), but I edited the code above to use some descriptive variables to try and make this more clear. If you want to be thorough, you should also check for ssh-dss, and possibly pgp-sign-rsa, and pgp-sign-dss, but they are far less common.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 404k
  • Answers 404k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think you've posted enough code to really understand… May 15, 2026 at 5:24 am
  • Editorial Team
    Editorial Team added an answer balsamiq http://www.balsamiq.com/products/mockups May 15, 2026 at 5:24 am
  • Editorial Team
    Editorial Team added an answer you can force a MPMoviePlayerViewcontroller to work in landscapce mode… May 15, 2026 at 5:24 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.