Forked from TomMaddox/JuiceSSH-validateKeyFile(File)
Created
December 6, 2019 13:13
-
-
Save SecurityAnalysts/2614c3493a4b5e3cd51c96e28e5dfecf to your computer and use it in GitHub Desktop.
JuiceSSH Validate Key File
This file contains hidden or 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
| public static boolean validateKeyFile(File fileHandle) { | |
| final int fileSizeLimitKB = 8; | |
| if (fileHandle.length() > fileSizeLimitKB * 1024) { | |
| return false; | |
| } | |
| try { | |
| InputStream is = new FileInputStream(fileHandle); | |
| BufferedReader reader = new BufferedReader(new InputStreamReader(is)); | |
| char[] header = new char[37]; | |
| reader.read(header, 0, 37); | |
| reader.close(); | |
| if(String.valueOf(header).contains("-----BEGIN RSA PRIVATE KEY-----")) { | |
| return true; | |
| } | |
| if(String.valueOf(header).contains("-----BEGIN DSA PRIVATE KEY-----")) { | |
| return true; | |
| } | |
| if(String.valueOf(header).contains("-----BEGIN PRIVATE KEY-----")) { | |
| return true; | |
| } | |
| if(String.valueOf(header).contains("-----BEGIN ENCRYPTED PRIVATE KEY-----")) { | |
| return true; | |
| } | |
| } catch (FileNotFoundException e) { | |
| Log.e(TAG, "File not found"); | |
| return false; | |
| } catch (IOException e) { | |
| Log.e(TAG, "Not a textfile"); | |
| return false; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment