Skip to content

Instantly share code, notes, and snippets.

@vcgato29
Forked from kaushikthedeveloper/Ssh2Converter.java
Created January 16, 2019 10:42
Show Gist options
  • Save vcgato29/15e8c8ffb21a036645dd49dbe5dc1fb8 to your computer and use it in GitHub Desktop.
Save vcgato29/15e8c8ffb21a036645dd49dbe5dc1fb8 to your computer and use it in GitHub Desktop.
Convert OpenSSH public key to RFC 4716 (SSH2) format
/**
* Convert OpenSSH to RFC 4716 (SSH2) format [https://tools.ietf.org/html/rfc4716]
*
* @author KaushikTheDeveloper
* Created on Mar 08, 2018
*/
public class Ssh2Converter {
private static final int MAX_LINE_LENGTH = 70;
private static final String BEGIN_MARKER = "---- BEGIN SSH2 PUBLIC KEY ----\n";
private static final String END_MARKER = "---- END SSH2 PUBLIC KEY ----";
private static final String STANDARD_COMMENT = "Comment: \"2048-bit RSA, converted from OpenSSH";
private static final String ADDITIONAL_COMMENT = " \\\nfor %s\"\n";
private static final String NEW_LINE = "\n";
private StringBuilder ssh2PublicKey;
Ssh2Converter() {
ssh2PublicKey = new StringBuilder();
}
/**
* Convert Openssh to SSH2 (RFC 4716 compliant)
*
* @param opensshKey : the public key in openssh format
* @return SSH2 public key
*/
public String convert(String opensshKey) {
try {
ssh2PublicKey.append(BEGIN_MARKER);
String[] opensshSplit = opensshKey.split(" ");
appendComment(opensshSplit);
appendHash(opensshSplit[1]);
ssh2PublicKey.append(END_MARKER);
return ssh2PublicKey.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Append the required comment
* If comment was provided in openssh :
* - Append Standard comment + Additional comment provided ;true
* - Append Standard comment + close the double quotes ;false
*
* @param opensshSplit : openssh key split by whitespace
*/
private void appendComment(String[] opensshSplit) {
ssh2PublicKey.append(STANDARD_COMMENT);
// comment is available in the openssh key
if (opensshSplit.length == 3) {
ssh2PublicKey.append(String.format(ADDITIONAL_COMMENT, opensshSplit[2]));
} else {
ssh2PublicKey.append("\"\n");
}
}
/**
* Append the hash of the OpenSSH key
* GUIDELINE : each line in the body MUST NOT be longer than 72 8-bit bytes excluding line termination characters
* => Split the body at 70 chars and wrap to next line
*
* @param hash : OpenSSH Public Key Body
*/
private void appendHash(String hash) {
int index = 0;
while (index < hash.length()) {
ssh2PublicKey.append(
hash.substring(index, Math.min(index + MAX_LINE_LENGTH, hash.length()))
);
ssh2PublicKey.append(NEW_LINE);
index += MAX_LINE_LENGTH;
}
}
}
/**
* Driver function :
* - With comment in OpenSSH public key
* - Without comment in OpenSSH public key
*
* Example key taken from [https://tools.ietf.org/html/rfc4716]
*/
class DriverFunction {
public static void main(String[] args) {
final String opensshKey1 = "ssh-rsa " +
"AAAAB3NzaC1yc2EAAAABIwAAAIEA1on8gxCGJJWSRT4uOrR13mUaUk0hRf4RzxSZ1zRbYY" +
"Fw8pfGesIFoEuVth4HKyF8k1y4mRUnYHP1XNMNMJl1JcEArC2asV8sHf6zSPVffozZ5TT4" +
"SfsUu/iKy9lUcCfXzwre4WWZSXXcPff+EHtWshahu3WzBdnGxm5Xoi89zcE=" +
" me@example.com";
final String expected1 = "---- BEGIN SSH2 PUBLIC KEY ----\n" +
"Comment: \"2048-bit RSA, converted from OpenSSH \\\n" +
"for me@example.com\"\n" +
"AAAAB3NzaC1yc2EAAAABIwAAAIEA1on8gxCGJJWSRT4uOrR13mUaUk0hRf4RzxSZ1zRbYY\n" +
"Fw8pfGesIFoEuVth4HKyF8k1y4mRUnYHP1XNMNMJl1JcEArC2asV8sHf6zSPVffozZ5TT4\n" +
"SfsUu/iKy9lUcCfXzwre4WWZSXXcPff+EHtWshahu3WzBdnGxm5Xoi89zcE=\n" +
"---- END SSH2 PUBLIC KEY ----";
final String result1 = new Ssh2Converter().convert(opensshKey1);
if (!expected1.equals(result1))
throw new RuntimeException("Assertion Failed");
final String opensshKey2 = "ssh-rsa " +
"AAAAB3NzaC1yc2EAAAABIwAAAIEA1on8gxCGJJWSRT4uOrR13mUaUk0hRf4RzxSZ1zRbYY" +
"Fw8pfGesIFoEuVth4HKyF8k1y4mRUnYHP1XNMNMJl1JcEArC2asV8sHf6zSPVffozZ5TT4" +
"SfsUu/iKy9lUcCfXzwre4WWZSXXcPff+EHtWshahu3WzBdnGxm5Xoi89zcE=";
final String expected2 = "---- BEGIN SSH2 PUBLIC KEY ----\n" +
"Comment: \"2048-bit RSA, converted from OpenSSH\"\n" +
"AAAAB3NzaC1yc2EAAAABIwAAAIEA1on8gxCGJJWSRT4uOrR13mUaUk0hRf4RzxSZ1zRbYY\n" +
"Fw8pfGesIFoEuVth4HKyF8k1y4mRUnYHP1XNMNMJl1JcEArC2asV8sHf6zSPVffozZ5TT4\n" +
"SfsUu/iKy9lUcCfXzwre4WWZSXXcPff+EHtWshahu3WzBdnGxm5Xoi89zcE=\n" +
"---- END SSH2 PUBLIC KEY ----";
final String result2 = new Ssh2Converter().convert(opensshKey2);
if (!expected2.equals(result2))
throw new RuntimeException("Assertion Failed");
}
}
@vcgato29
Copy link
Author

I am going To Implement Oracles Software

@vcgato29
Copy link
Author

If Correctthis is my eXcemtion of the license open-ssh

@vcgato29
Copy link
Author

i forgot the p in excemption

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment