Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code improvements #9

Open
wants to merge 5 commits into
base: official
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/src/main/java/ch/threema/apitool/APIConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public UploadResult uploadFile(EncryptResult fileEncryptionResult) throws IOExc
request.write(fileEncryptionResult.getResult());
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
request.close();

String response = null;
int responseCode = connection.getResponseCode();
Expand Down
2 changes: 1 addition & 1 deletion source/src/main/java/ch/threema/apitool/ConsoleMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static class ArgumentCommand {

public void run(String[] givenArguments) throws Exception {
if(givenArguments.length < this.arguments.length) {
throw new Exception("invalid arguments");
throw new IllegalArgumentException("invalid arguments");
}

this.command.run((String[]) ArrayUtils.subarray(givenArguments, this.arguments.length, givenArguments.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.LinkedList;
import java.util.List;

abstract public class Command {
public abstract class Command {
private final List<Field> fields = new LinkedList<>();
private final String subject;
private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected void execute() throws Exception {
System.out.println("Remaining credits: " + credits);
}
else {
System.out.println("Error fetching credits");;
System.out.println("Error fetching credits");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
* A file message that can be sent/received with end-to-end encryption via Threema.
*/
public class FileMessage extends ThreemaMessage {
private final static String KEY_BLOB_ID = "b";
private final static String KEY_THUMBNAIL_BLOB_ID = "t";
private final static String KEY_ENCRYPTION_KEY = "k";
private final static String KEY_MIME_TYPE = "m";
private final static String KEY_FILE_NAME = "n";
private final static String KEY_FILE_SIZE = "s";
private final static String KEY_TYPE = "i";
private static final String KEY_BLOB_ID = "b";
private static final String KEY_THUMBNAIL_BLOB_ID = "t";
private static final String KEY_ENCRYPTION_KEY = "k";
private static final String KEY_MIME_TYPE = "m";
private static final String KEY_FILE_NAME = "n";
private static final String KEY_FILE_SIZE = "s";
private static final String KEY_TYPE = "i";

public static final int TYPE_CODE = 0x17;

Expand Down
16 changes: 8 additions & 8 deletions source/src/test/java/ch/threema/apitool/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
*/
public abstract class Common {

public static final String myPrivateKey = "private:94af3260fa2a19adc8e82e82be598be15bc6ad6f47c8ee303cb185ef860e16d2";
public static final String myPrivateKeyExtract = "94af3260fa2a19adc8e82e82be598be15bc6ad6f47c8ee303cb185ef860e16d2";
public static final String MY_PRIVATE_KEY = "private:94af3260fa2a19adc8e82e82be598be15bc6ad6f47c8ee303cb185ef860e16d2";
public static final String MY_PRIVATE_KEY_EXTRACT = "94af3260fa2a19adc8e82e82be598be15bc6ad6f47c8ee303cb185ef860e16d2";

public static final String myPublicKey = "public:3851ad59c96146a05b32e41c0ccd0fd639dc8cd66bf6e1cbd3c8d67e4e8f5531";
public static final String myPublicKeyExtract = "3851ad59c96146a05b32e41c0ccd0fd639dc8cd66bf6e1cbd3c8d67e4e8f5531";
public static final String MY_PUBLIC_KEY = "public:3851ad59c96146a05b32e41c0ccd0fd639dc8cd66bf6e1cbd3c8d67e4e8f5531";
public static final String MY_PUBLIC_KEY_EXTRACT = "3851ad59c96146a05b32e41c0ccd0fd639dc8cd66bf6e1cbd3c8d67e4e8f5531";

public static final String otherPrivateKey = "private:8318e05220acd38e97ba41a9a6318688214219916075ca060f9339a6d1f7fc29";
public static final String otherPublicKey = "public:10ac7fd937eafb806f9a05bf9afa340a99387b0063cc9cb0d1ea5505d39cc076";
public static final String OTHER_PRIVATE_KEY = "private:8318e05220acd38e97ba41a9a6318688214219916075ca060f9339a6d1f7fc29";
public static final String OTHER_PUBLIC_KEY = "public:10ac7fd937eafb806f9a05bf9afa340a99387b0063cc9cb0d1ea5505d39cc076";

public static final String echochoPublicKey = "public:4a6a1b34dcef15d43cb74de2fd36091be99fbbaf126d099d47d83d919712c72b";
public static final String randomNonce = "516f4f1562dda0704a7bae8997cf0b354c6980181152ac32";
public static final String ECHOCHO_PUBLIC_KEY = "public:4a6a1b34dcef15d43cb74de2fd36091be99fbbaf126d099d47d83d919712c72b";
public static final String RANDOM_NONCE = "516f4f1562dda0704a7bae8997cf0b354c6980181152ac32";

public static boolean isEmpty(byte[] byteArray) {
if(byteArray == null) {
Expand Down
12 changes: 6 additions & 6 deletions source/src/test/java/ch/threema/apitool/CryptToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void testDecrypt() throws Exception {
String nonce = "0a1ec5b67b4d61a1ef91f55e8ce0471fee96ea5d8596dfd0";
String box = "45181c7aed95a1c100b1b559116c61b43ce15d04014a805288b7d14bf3a993393264fe554794ce7d6007233e8ef5a0f1ccdd704f34e7c7b77c72c239182caf1d061d6fff6ffbbfe8d3b8f3475c2fe352e563aa60290c666b2e627761e32155e62f048b52ef2f39c13ac229f393c67811749467396ecd09f42d32a4eb419117d0451056ac18fac957c52b0cca67568e2d97e5a3fd829a77f914a1ad403c5909fd510a313033422ea5db71eaf43d483238612a54cb1ecfe55259b1de5579e67c6505df7d674d34a737edf721ea69d15b567bc2195ec67e172f3cb8d6842ca88c29138cc33e9351dbc1e4973a82e1cf428c1c763bb8f3eb57770f914a";

Key privateKey = Key.decodeKey(Common.otherPrivateKey);
Key publicKey = Key.decodeKey(Common.myPublicKey);
Key privateKey = Key.decodeKey(Common.OTHER_PRIVATE_KEY);
Key publicKey = Key.decodeKey(Common.MY_PUBLIC_KEY);

ThreemaMessage message = CryptTool.decryptMessage(
DataUtils.hexStringToByteArray(box),
Expand All @@ -79,8 +79,8 @@ public void testEncrypt() throws Exception {
String text = "Dies ist eine Testnachricht. äöü";
String nonce = "0a1ec5b67b4d61a1ef91f55e8ce0471fee96ea5d8596dfd0";

Key privateKey = Key.decodeKey(Common.myPrivateKey);
Key publicKey = Key.decodeKey(Common.otherPublicKey);
Key privateKey = Key.decodeKey(Common.MY_PRIVATE_KEY);
Key publicKey = Key.decodeKey(Common.OTHER_PUBLIC_KEY);

EncryptResult res = CryptTool.encryptTextMessage(text, privateKey.key, publicKey.key);
Assert.assertNotNull(res);
Expand All @@ -92,8 +92,8 @@ public void testEncrypt() throws Exception {

@Test
public void testDerivePublicKey() throws Exception{
Key privateKey = Key.decodeKey(Common.myPrivateKey);
Key publicKey = Key.decodeKey(Common.myPublicKey);
Key privateKey = Key.decodeKey(Common.MY_PRIVATE_KEY);
Key publicKey = Key.decodeKey(Common.MY_PUBLIC_KEY);
byte[] derivedPublicKey = CryptTool.derivePublicKey(privateKey.key);
Assert.assertNotNull("derived public key", derivedPublicKey);
Assert.assertEquals(derivedPublicKey, publicKey.key);
Expand Down
4 changes: 2 additions & 2 deletions source/src/test/java/ch/threema/apitool/KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public void testDecodeKeyPublic() throws Exception {

@Test
public void testEncodePrivate() throws Exception {
byte[] keyAsByte = DataUtils.hexStringToByteArray(Common.myPrivateKeyExtract);
byte[] keyAsByte = DataUtils.hexStringToByteArray(Common.MY_PRIVATE_KEY_EXTRACT);

Key key = new Key(Key.KeyType.PRIVATE, keyAsByte);
Assert.assertNotNull("key instance", key);

Assert.assertEquals(Key.KeyType.PRIVATE, key.type);
Assert.assertEquals(key.key, keyAsByte);

Assert.assertEquals(key.encode(), Common.myPrivateKey);
Assert.assertEquals(key.encode(), Common.MY_PRIVATE_KEY);
}
}