Last active
October 6, 2023 14:15
-
-
Save piyushbansal/5243418 to your computer and use it in GitHub Desktop.
A simple script to validate SSH keys.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash/python | |
# Author : Piyush Bansal | |
# To check if a key is valid ssh key (rsa) | |
# REQUIREMENTS : give input a file , that has one ssh-key in it. | |
# RESULT : valid/not valid ssh-key | |
# NOTE : initial devel. phase, needs to to tweaked to make it suitable for our use here. | |
import base64,struct,sys,binascii | |
key=open('input.txt').read() | |
array=key.split(); | |
# Each rsa-ssh key has 3 different strings in it, first one being | |
# typeofkey second one being keystring third one being username . | |
if len(array) != 3: | |
print "not valid key" | |
sys.exit() | |
typeofkey=array[0];string=array[1];username=array[2]; | |
# must have only valid rsa-ssh key characters ie binascii characters | |
try : | |
data=base64.decodestring(string) | |
except binascii.Error: | |
print "not valid key" | |
sys.exit() | |
a=4 | |
# unpack the contents of data, from data[:4] , it must be equal to 7 , property of ssh key . | |
try : | |
str_len = struct.unpack('>I', data[:a])[0] | |
except struct.error : | |
print "not valid key" | |
sys.exit() | |
# data[4:11] must have string which matches with the typeofkey , another ssh key property. | |
if data[a:a+str_len] == typeofkey and int(str_len) == int(7): | |
print "valid key" | |
else: | |
print "not valid key" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment