/** * Returns {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * * @param supplier * the input stream factory, formatted {@code ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB...} * * @return the {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * @throws IOException * if an I/O error occurs */ public static RSAPublicKeySpec publicKeySpecFromOpenSSH(InputSupplier<? extends InputStream> supplier) throws IOException { InputStream stream = supplier.getInput(); Iterable<String> parts = Splitter.on(' ').split(toStringAndClose(stream).trim()); checkArgument(size(parts) >= 2 && "ssh-rsa".equals(get(parts, 0)), "bad format, should be: ssh-rsa AAAAB3..."); stream = new ByteArrayInputStream(base64().decode(get(parts, 1))); String marker = new String(readLengthFirst(stream)); checkArgument("ssh-rsa".equals(marker), "looking for marker ssh-rsa but got %s", marker); BigInteger publicExponent = new BigInteger(readLengthFirst(stream)); BigInteger modulus = new BigInteger(readLengthFirst(stream)); return new RSAPublicKeySpec(modulus, publicExponent); }
/** * Returns {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * * @param supplier * the input stream factory, formatted {@code ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB...} * * @return the {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * @throws IOException * if an I/O error occurs */ public static RSAPublicKeySpec publicKeySpecFromOpenSSH(ByteSource supplier) throws IOException { InputStream stream = supplier.openStream(); Iterable<String> parts = Splitter.on(' ').split(toStringAndClose(stream).trim()); checkArgument(size(parts) >= 2 && "ssh-rsa".equals(get(parts, 0)), "bad format, should be: ssh-rsa AAAAB3..."); stream = new ByteArrayInputStream(base64().decode(get(parts, 1))); String marker = new String(readLengthFirst(stream)); checkArgument("ssh-rsa".equals(marker), "looking for marker ssh-rsa but got %s", marker); BigInteger publicExponent = new BigInteger(readLengthFirst(stream)); BigInteger modulus = new BigInteger(readLengthFirst(stream)); return new RSAPublicKeySpec(modulus, publicExponent); }
/** * Returns {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * * @param supplier * the input stream factory, formatted {@code ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB...} * * @return the {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * @throws IOException * if an I/O error occurs */ public static RSAPublicKeySpec publicKeySpecFromOpenSSH(InputSupplier<? extends InputStream> supplier) throws IOException { InputStream stream = supplier.getInput(); Iterable<String> parts = Splitter.on(' ').split(toStringAndClose(stream).trim()); checkArgument(size(parts) >= 2 && "ssh-rsa".equals(get(parts, 0)), "bad format, should be: ssh-rsa AAAAB3..."); stream = new ByteArrayInputStream(base64().decode(get(parts, 1))); String marker = new String(readLengthFirst(stream)); checkArgument("ssh-rsa".equals(marker), "looking for marker ssh-rsa but got %s", marker); BigInteger publicExponent = new BigInteger(readLengthFirst(stream)); BigInteger modulus = new BigInteger(readLengthFirst(stream)); return new RSAPublicKeySpec(modulus, publicExponent); }
/** * Returns {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * * @param supplier * the input stream factory, formatted {@code ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB...} * * @return the {@link RSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * @throws IOException * if an I/O error occurs */ public static RSAPublicKeySpec publicKeySpecFromOpenSSH(ByteSource supplier) throws IOException { InputStream stream = supplier.openStream(); Iterable<String> parts = Splitter.on(' ').split(toStringAndClose(stream).trim()); checkArgument(size(parts) >= 2 && "ssh-rsa".equals(get(parts, 0)), "bad format, should be: ssh-rsa AAAAB3..."); stream = new ByteArrayInputStream(base64().decode(get(parts, 1))); String marker = new String(readLengthFirst(stream)); checkArgument("ssh-rsa".equals(marker), "looking for marker ssh-rsa but got %s", marker); BigInteger publicExponent = new BigInteger(readLengthFirst(stream)); BigInteger modulus = new BigInteger(readLengthFirst(stream)); return new RSAPublicKeySpec(modulus, publicExponent); }