diff options
author | Kenny Root <kenny@the-b.org> | 2013-04-14 16:35:21 -0700 |
---|---|---|
committer | Kenny Root <kenny@the-b.org> | 2013-04-14 16:35:21 -0700 |
commit | c6cc322ab28ba55c0253c40b0774bba5a751bc35 (patch) | |
tree | 2a3bbaa4f9887fb34e6e523c7d1966bdd61aa167 /src | |
parent | fc0fd37d666bb8bd1452c2263cd6fdcf652230cd (diff) | |
download | connectbot-c6cc322ab28ba55c0253c40b0774bba5a751bc35.tar.gz connectbot-c6cc322ab28ba55c0253c40b0774bba5a751bc35.tar.bz2 connectbot-c6cc322ab28ba55c0253c40b0774bba5a751bc35.zip |
Split recoverKeyPair method for testing
Diffstat (limited to 'src')
-rw-r--r-- | src/org/connectbot/util/PubkeyUtils.java | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/org/connectbot/util/PubkeyUtils.java b/src/org/connectbot/util/PubkeyUtils.java index 314624b..6e390b6 100644 --- a/src/org/connectbot/util/PubkeyUtils.java +++ b/src/org/connectbot/util/PubkeyUtils.java @@ -199,7 +199,8 @@ public class PubkeyUtils { } } - public static KeyPair recoverKeyPair(byte[] encoded) throws NoSuchAlgorithmException, InvalidKeySpecException { + public static KeyPair recoverKeyPair(byte[] encoded) throws NoSuchAlgorithmException, + InvalidKeySpecException { final String algo = getAlgorithmForOid(getOidFromPkcs8Encoded(encoded)); final KeySpec privKeySpec = new PKCS8EncodedKeySpec(encoded); @@ -207,10 +208,15 @@ public class PubkeyUtils { final KeyFactory kf = KeyFactory.getInstance(algo); final PrivateKey priv = kf.generatePrivate(privKeySpec); - final PublicKey pub; + return new KeyPair(recoverPublicKey(kf, priv), priv); + } + + static PublicKey recoverPublicKey(KeyFactory kf, PrivateKey priv) + throws NoSuchAlgorithmException, InvalidKeySpecException { if (priv instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey rsaPriv = (RSAPrivateCrtKey) priv; - pub = kf.generatePublic(new RSAPublicKeySpec(rsaPriv.getModulus(), rsaPriv.getPublicExponent())); + return kf.generatePublic(new RSAPublicKeySpec(rsaPriv.getModulus(), rsaPriv + .getPublicExponent())); } else if (priv instanceof DSAPrivateKey) { DSAPrivateKey dsaPriv = (DSAPrivateKey) priv; DSAParams params = dsaPriv.getParams(); @@ -218,25 +224,22 @@ public class PubkeyUtils { // Calculate public key Y BigInteger y = params.getG().modPow(dsaPriv.getX(), params.getP()); - pub = kf.generatePublic(new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG())); + return kf.generatePublic(new DSAPublicKeySpec(y, params.getP(), params.getQ(), params + .getG())); } else if (priv instanceof ECPrivateKey) { ECPrivateKey ecPriv = (ECPrivateKey) priv; ECParameterSpec params = ecPriv.getParams(); // Calculate public key Y ECPoint generator = params.getGenerator(); - BigInteger[] wCoords = EcCore.multiplyPointA(new BigInteger[] { - generator.getAffineX(), - generator.getAffineY() }, - ecPriv.getS(), params); + BigInteger[] wCoords = EcCore.multiplyPointA(new BigInteger[] { generator.getAffineX(), + generator.getAffineY() }, ecPriv.getS(), params); ECPoint w = new ECPoint(wCoords[0], wCoords[1]); - pub = kf.generatePublic(new ECPublicKeySpec(w, params)); + return kf.generatePublic(new ECPublicKeySpec(w, params)); } else { - throw new NoSuchAlgorithmException("Unknown algorithm: " + algo); + throw new NoSuchAlgorithmException("Key type must be RSA, DSA, or EC"); } - - return new KeyPair(pub, priv); } /* |