Skip to content
Merged
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
15 changes: 7 additions & 8 deletions src/main/java/com/pusher/client/crypto/nacl/SecretBoxOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ a copy of this software and associated documentation files (the "Software"),

package com.pusher.client.crypto.nacl;

import static com.pusher.client.util.internal.Preconditions.checkArgument;
import static com.pusher.client.util.internal.Preconditions.checkNotNull;

import java.util.Arrays;

public class SecretBoxOpener {
Expand All @@ -33,19 +36,15 @@ public class SecretBoxOpener {
private byte[] key;

public SecretBoxOpener(byte[] key) {
// TODO: add a a little preconditions lib/class for that
if (key == null) throw new IllegalArgumentException("null key passed");
if (key.length != 32) {
throw new IllegalArgumentException("key should be 32B, is: " + key.length + "B");
}
checkNotNull(key, "null key passed");
checkArgument(key.length == 32, "key length must be 32 bytes, but is " +
key.length + " bytes");

this.key = key;
}

public byte[] open(byte box[], byte nonce[]) throws AuthenticityException {
if (key == null) {
throw new IllegalStateException("key has been cleared, create new instance");
}
checkNotNull(key, "key has been cleared, create new instance");

byte subKey[] = new byte[32];
byte counter[] = new byte[16];
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/pusher/client/util/internal/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pusher.client.util.internal;

public class Preconditions {

public static void checkArgument(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}

public static void checkState(boolean expression, Object errorMessage) {
if (!expression) {
throw new IllegalStateException(String.valueOf(errorMessage));
}
}

public static <T> T checkNotNull(T reference, Object errorMessage) {
if (reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
}
return reference;
}
}