Validating ssh public keys is easy!
If you want to know if a (say, user-supplied) ssh public key is valid, this is easy to do.
The keys have a standard format which is:
key-type base64-encoded-key comment
The base64 encoded key, when decoded, yields a string beginning with four bytes describing an integer (big-endian). That integer states the length of a string immediately following. In nearly all cases, it's going to be a ssh-rsa key and look something like this:
00 00 00 07 73 73 68 2d 72 73 61 00 ....ssh-rsa.
Here's an implementation in Python. All of the imported modules are in the standard library.
def is_valid_key(content): import struct, base64, binascii try: key_type, key_string, comment = content.split() data = base64.decodestring(key_string) int_len = 4 str_len = struct.unpack('>I', data[:int_len])[0] return data[int_len:int_len+str_len] == key_type except binascii.Error: return False except ValueError, err: if 'unpack' in err.message: return False raise