/** * @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)); }
@Test public void testCanGenerate() { Map<String, String> map = generate(); assert map.get("public").startsWith("ssh-rsa ") : map; assert map.get("private").startsWith("-----BEGIN RSA PRIVATE KEY-----") : map; assert privateKeyMatchesPublicKey(map.get("private"), map.get("public")) : map; }
@Test public void testCanGenerate() { Map<String, String> map = generate(); assert map.get("public").startsWith("ssh-rsa ") : map; assert map.get("private").startsWith("-----BEGIN RSA PRIVATE KEY-----") : map; assert privateKeyMatchesPublicKey(map.get("private"), map.get("public")) : map; }
@Test public void testPrivateKeyMatchesPublicKeyString() throws IOException { String privKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test")); String pubKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")); assert privateKeyMatchesPublicKey(privKey, pubKey); }
@Test public void testPrivateKeyMatchesPublicKeyString() throws IOException { String privKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test")); String pubKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")); assert privateKeyMatchesPublicKey(privKey, pubKey); }
@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 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); }
assertTrue(SshKeys.privateKeyMatchesPublicKey(privateKey, publicKey)); assertEquals(SshKeys.fingerprintPublicKey(publicKey), expectedFingerprint); assertEquals(SshKeys.fingerprintPrivateKey(privateKey), expectedFingerprint);
assertTrue(SshKeys.privateKeyMatchesPublicKey(privateKey, publicKey)); assertEquals(SshKeys.fingerprintPublicKey(publicKey), expectedFingerprint); assertEquals(SshKeys.fingerprintPrivateKey(privateKey), expectedFingerprint);