commit 5821895ae9f9194a05d5ac11f3cec4fd0ae752ff Author: Daniel Gultsch Date: Mon Mar 28 12:52:05 2016 +0200 initial working commit diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2fce84f --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + eu.siacs.conversations + ImageDownloader + 0.1 + + + + org.bouncycastle + bcprov-jdk15on + 1.54 + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + package + + shade + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + eu.siacs.conversations.Downloader + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 8 + 8 + + + + + \ No newline at end of file diff --git a/src/main/java/eu/siacs/conversations/Downloader.java b/src/main/java/eu/siacs/conversations/Downloader.java new file mode 100644 index 0000000..d81cc42 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/Downloader.java @@ -0,0 +1,62 @@ +package eu.siacs.conversations; + +import org.bouncycastle.crypto.engines.AESEngine; +import org.bouncycastle.crypto.io.CipherOutputStream; +import org.bouncycastle.crypto.modes.AEADBlockCipher; +import org.bouncycastle.crypto.modes.GCMBlockCipher; +import org.bouncycastle.crypto.params.AEADParameters; +import org.bouncycastle.crypto.params.KeyParameter; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.HttpURLConnection; +import java.net.URL; + +public class Downloader { + + public static void main(String[] args) { + if (args.length != 1) { + System.err.println("Usage: java -jar ImageDownloader.jar https://url.tld/filename#C0FFEBABE"); + } else { + try { + final URL url = new URL(args[0]); + OutputStream os = setupOutputStream(new PrintStream(System.out), url.getRef()); + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + InputStream inputStream = connection.getInputStream(); + connection.connect(); + byte[] buffer = new byte[4096]; + int count; + while ((count = inputStream.read(buffer)) != -1) { + os.write(buffer, 0, count); + } + } catch (Exception e) { + System.err.println("could not connect to host "+e.getMessage()); + } + } + } + + private static OutputStream setupOutputStream(OutputStream os, String reference) { + if (reference != null && reference.length() == 96) { + byte[] keyAndIv = hexToBytes(reference); + byte[] key = new byte[32]; + byte[] iv = new byte[16]; + System.arraycopy(keyAndIv, 0, iv, 0, 16); + System.arraycopy(keyAndIv, 16, key, 0, 32); + AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); + cipher.init(false, new AEADParameters(new KeyParameter(key), 128, iv)); + return new CipherOutputStream(os, cipher); + } else { + return os; + } + } + + private static byte[] hexToBytes(String hex) { + int len = hex.length(); + byte[] array = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + array[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); + } + return array; + } +}