Search in sources :

Example 1 with SshException

use of org.apache.sshd.common.SshException in project gerrit by GerritCodeReview.

the class SshUtil method parse.

/**
 * Parse a public key into its Java type.
 *
 * @param key the account key to parse.
 * @return the valid public key object.
 * @throws InvalidKeySpecException the key supplied is not a valid SSH key.
 * @throws NoSuchAlgorithmException the JVM is missing the key algorithm.
 * @throws NoSuchProviderException the JVM is missing the provider.
 */
public static PublicKey parse(AccountSshKey key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    try {
        final String s = key.encodedKey();
        if (s == null) {
            throw new InvalidKeySpecException("No key string");
        }
        final byte[] bin = BaseEncoding.base64().decode(s);
        return new ByteArrayBuffer(bin).getRawPublicKey();
    } catch (RuntimeException | SshException e) {
        throw new InvalidKeySpecException("Cannot parse key", e);
    }
}
Also used : InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SshException(org.apache.sshd.common.SshException) ByteArrayBuffer(org.apache.sshd.common.util.buffer.ByteArrayBuffer)

Aggregations

InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 SshException (org.apache.sshd.common.SshException)1 ByteArrayBuffer (org.apache.sshd.common.util.buffer.ByteArrayBuffer)1