/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @param fingerprint * ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} * @return true if the keypair has the same fingerprint as supplied */ public static boolean publicKeyHasFingerprint(String publicKeyOpenSSH, String fingerprint) { return publicKeyHasFingerprint(publicKeySpecFromOpenSSH(publicKeyOpenSSH), fingerprint); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @param fingerprint * ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} * @return true if the keypair has the same fingerprint as supplied */ public static boolean publicKeyHasFingerprint(String publicKeyOpenSSH, String fingerprint) { return publicKeyHasFingerprint(publicKeySpecFromOpenSSH(publicKeyOpenSSH), fingerprint); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @param fingerprint * ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} * @return true if the keypair has the same fingerprint as supplied */ public static boolean publicKeyHasFingerprint(String publicKeyOpenSSH, String fingerprint) { return publicKeyHasFingerprint(publicKeySpecFromOpenSSH(publicKeyOpenSSH), fingerprint); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @param fingerprint * ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} * @return true if the keypair has the same fingerprint as supplied */ public static boolean publicKeyHasFingerprint(String publicKeyOpenSSH, String fingerprint) { return publicKeyHasFingerprint(publicKeySpecFromOpenSSH(publicKeyOpenSSH), fingerprint); }
/** * Executes {@link Pems#publicKeySpecFromOpenSSH(InputSupplier)} on the string which was OpenSSH * Base64 Encoded {@code id_rsa.pub} * * @param idRsaPub * formatted {@code ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB...} * @see Pems#publicKeySpecFromOpenSSH(InputSupplier) */ public static RSAPublicKeySpec publicKeySpecFromOpenSSH(String idRsaPub) { try { return publicKeySpecFromOpenSSH(ByteStreams.newInputStreamSupplier( idRsaPub.getBytes(Charsets.UTF_8))); } catch (IOException e) { throw propagate(e); } }
/** * Executes {@link Pems#publicKeySpecFromOpenSSH(ByteSource)} on the string which was OpenSSH * Base64 Encoded {@code id_rsa.pub} * * @param idRsaPub * formatted {@code ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB...} * @see Pems#publicKeySpecFromOpenSSH(ByteSource) */ public static RSAPublicKeySpec publicKeySpecFromOpenSSH(String idRsaPub) { try { return publicKeySpecFromOpenSSH(ByteSource.wrap( idRsaPub.getBytes(Charsets.UTF_8))); } catch (IOException e) { throw propagate(e); } }
/** * @param privateKeyPEM * RSA private key in PEM format * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return true if the keypairs match */ public static boolean privateKeyMatchesPublicKey(String privateKeyPEM, String publicKeyOpenSSH) { KeySpec privateKeySpec = privateKeySpec(privateKeyPEM); checkArgument(privateKeySpec instanceof RSAPrivateCrtKeySpec, "incorrect format expected RSAPrivateCrtKeySpec was %s", privateKeySpec); return privateKeyMatchesPublicKey(RSAPrivateCrtKeySpec.class.cast(privateKeySpec), publicKeySpecFromOpenSSH(publicKeyOpenSSH)); }
/** * @param privateKeyPEM * RSA private key in PEM format * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return true if the keypairs match */ public static boolean privateKeyMatchesPublicKey(String privateKeyPEM, String publicKeyOpenSSH) { KeySpec privateKeySpec = privateKeySpec(privateKeyPEM); checkArgument(privateKeySpec instanceof RSAPrivateCrtKeySpec, "incorrect format expected RSAPrivateCrtKeySpec was %s", privateKeySpec); return privateKeyMatchesPublicKey(RSAPrivateCrtKeySpec.class.cast(privateKeySpec), publicKeySpecFromOpenSSH(publicKeyOpenSSH)); }
/** * @param privateKeyPEM * RSA private key in PEM format * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return true if the keypairs match */ public static boolean privateKeyMatchesPublicKey(String privateKeyPEM, String publicKeyOpenSSH) { KeySpec privateKeySpec = privateKeySpec(privateKeyPEM); checkArgument(privateKeySpec instanceof RSAPrivateCrtKeySpec, "incorrect format expected RSAPrivateCrtKeySpec was %s", privateKeySpec); return privateKeyMatchesPublicKey(RSAPrivateCrtKeySpec.class.cast(privateKeySpec), publicKeySpecFromOpenSSH(publicKeyOpenSSH)); }
/** * @param privateKeyPEM * RSA private key in PEM format * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return true if the keypairs match */ public static boolean privateKeyMatchesPublicKey(String privateKeyPEM, String publicKeyOpenSSH) { KeySpec privateKeySpec = privateKeySpec(privateKeyPEM); checkArgument(privateKeySpec instanceof RSAPrivateCrtKeySpec, "incorrect format expected RSAPrivateCrtKeySpec was %s", privateKeySpec); return privateKeyMatchesPublicKey(RSAPrivateCrtKeySpec.class.cast(privateKeySpec), publicKeySpecFromOpenSSH(publicKeyOpenSSH)); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return fingerprint ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} */ public static String fingerprintPublicKey(String publicKeyOpenSSH) { RSAPublicKeySpec publicKeySpec = publicKeySpecFromOpenSSH(publicKeyOpenSSH); return fingerprint(publicKeySpec.getPublicExponent(), publicKeySpec.getModulus()); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return fingerprint ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} */ public static String fingerprintPublicKey(String publicKeyOpenSSH) { RSAPublicKeySpec publicKeySpec = publicKeySpecFromOpenSSH(publicKeyOpenSSH); return fingerprint(publicKeySpec.getPublicExponent(), publicKeySpec.getModulus()); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return fingerprint ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} */ public static String fingerprintPublicKey(String publicKeyOpenSSH) { RSAPublicKeySpec publicKeySpec = publicKeySpecFromOpenSSH(publicKeyOpenSSH); return fingerprint(publicKeySpec.getPublicExponent(), publicKeySpec.getModulus()); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return fingerprint ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} */ public static String fingerprintPublicKey(String publicKeyOpenSSH) { RSAPublicKeySpec publicKeySpec = publicKeySpecFromOpenSSH(publicKeyOpenSSH); return fingerprint(publicKeySpec.getPublicExponent(), publicKeySpec.getModulus()); }
@Test public void testEncodeAsOpenSSH() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { String encoded = SshKeys.encodeAsOpenSSH((RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic( SshKeys.publicKeySpecFromOpenSSH(Payloads.newPayload(getClass().getResourceAsStream("/test.pub"))))); assertEquals(encoded, Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")).trim()); }
@Test public void testEncodeAsOpenSSH() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { String encoded = SshKeys.encodeAsOpenSSH((RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic( SshKeys.publicKeySpecFromOpenSSH(Resources.asByteSource(Resources.getResource(getClass(), "/test.pub"))))); assertEquals(encoded, Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")).trim()); }
@Test public void testPrivateKeyMatchesPublicKeyTyped() throws IOException { String privKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test")); RSAPrivateCrtKeySpec privateKey = (RSAPrivateCrtKeySpec) Pems.privateKeySpec(privKey); String pubKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")); RSAPublicKeySpec publicKey = publicKeySpecFromOpenSSH(pubKey); assert privateKeyMatchesPublicKey(privateKey, publicKey); }
@Test public void testCanReadRsaAndCompareFingerprintOnPublicRSAKey() throws IOException { String pubKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")); RSAPublicKeySpec key = SshKeys.publicKeySpecFromOpenSSH(pubKey); String fingerPrint = fingerprint(key.getPublicExponent(), key.getModulus()); assertEquals(fingerPrint, expectedFingerprint); }
@Test public void testPrivateKeyMatchesPublicKeyTyped() throws IOException { String privKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test")); RSAPrivateCrtKeySpec privateKey = (RSAPrivateCrtKeySpec) Pems.privateKeySpec(privKey); String pubKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")); RSAPublicKeySpec publicKey = publicKeySpecFromOpenSSH(pubKey); assert privateKeyMatchesPublicKey(privateKey, publicKey); }
@Test public void testCanReadRsaAndCompareFingerprintOnPublicRSAKey() throws IOException { String pubKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")); RSAPublicKeySpec key = SshKeys.publicKeySpecFromOpenSSH(pubKey); String fingerPrint = fingerprint(key.getPublicExponent(), key.getModulus()); assertEquals(fingerPrint, expectedFingerprint); }